导入不适用于 tsconfig.json 中的 "module": "ESNEXT"

2023-12-31

我在服务器端有以下代码:

import * as express from "express";

export class AppRouter {
  private static instance: express.Router;

  static getInstance(): express.Router {
    if (!AppRouter.instance) {
      AppRouter.instance = express.Router();
    }

    return AppRouter.instance;
  }
}

VSCODE 编译器不会在上述代码上显示任何错误。但是,当我运行它时,它显示以下错误:

import * as express from "express";
^^^^^^

SyntaxError: Cannot use import statement outside a module

当模块在 tsconfig.json 中设置为 commonJS 时效果很好。

但由于某种原因,我需要使用ESNEXT进行模块设置。

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNEXT",
    "module": "ESNEXT",
    "allowJs": true,
    "jsx": "react",
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "include": [
    "src/**/*.ts", "src/**/*.tsx"
  ],
  "exclude": [
    "node_modules",
    ".vscode"
  ]
}

我该如何解决这个问题?

- - - - - - 添加 - - - - - - -

包.json:

{
  "name": "APP",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "dev": "NODE_ENV=development nodemon",
    "prod": "NODE_ENV=production ts-node ./src/server/main.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/react": "16.8.19",
    "@types/react-dom": "16.8.4",
    "@types/react-redux": "^7.0.9",
    "@types/react-router-dom": "^5.1.3",
    "@types/react-router": "^5.1.3",
    "@types/webpack-env": "^1.14.1",
    "@types/body-parser": "^1.17.0",
    "@types/express": "^4.16.1",
    "@types/node": "^12.12.21",
    "babel-core": "^6.26.3",
    "babel-preset-stage-2": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "babel-loader": "^7.1.4",
    "babel-plugin-universal-import": "^4.0.0",
    "babel-plugin-transform-async-to-promises": "^0.6.1",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-plugin-async-to-promises": "^1.0.5",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-redux": "^7.0.3",
    "react-router": "^5.0.1",
    "react-router-dom": "^5.0.1",
    "react-universal-component": "^4.0.0",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "serialize-javascript": "^2.1.0",
    "history": "^4.10.1",
    "redux-form": "^8.2.6",
    "react-helmet": "^5.2.1",
    "url-loader": "^1.0.1",
    "markdown-with-front-matter-loader": "^0.1.0",
    "iltorb": "^2.3.2",
    "front-matter-loader": "^0.2.0",
    "file-loader": "^1.1.11",
    "css-loader": "^0.28.11",
    "compression-webpack-plugin": "^1.1.11",
    "brotli-webpack-plugin": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "optimize-css-assets-webpack-plugin": "^4.0.1",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "cors": "^2.7.1",
    "react-hot-loader": "^4.1.2",
    "extract-css-chunks-webpack-plugin": "^3.0.6",
    "webpack-hot-server-middleware": "^0.5.0",
    "express-static-gzip": "^0.3.2",
    "ts-loader": "^4.0.0",
    "webpack-flush-chunks": "^3.0.0-alpha.4",
    "webpack-hot-middleware": "^2.22.1",
    "webpack": "^4.8.3",
    "webpack-bundle-analyzer": "^2.11.1",
    "typescript": "^3.7.4",
    "webpack-dev-middleware": "^3.7.2",
    "ts-node": "^8.5.4",
    "express": "^4.17.1",
    "nodemon": "^1.19.4",
    "body-parser": "^1.19.0",
    "axios": "^0.16.2",
    "js-cookie": "^2.2.0",
    "react-scroll": "^1.7.14",
    "react-animate-on-scroll": "^2.1.5",
    "react-iframe": "^1.8.0",
    "react-fade-in": "^0.1.6",
    "react-loading": "^2.0.3",
    "rodal": "^1.6.3",
    "react-lottie": "^1.2.3",
    "react-google-login": "^5.0.2",
    "moment": "^2.24.0",
    "cross-fetch": "^2.1.1",
    "yaml-front-matter": "^4.0.0",
    "marked": "^0.3.19",
    "@types/cookie-session": "^2.0.37",
    "concurrently": "^4.1.0",
    "cookie-session": "^1.3.3",
    "reflect-metadata": "^0.1.13"
  },
  "devDependencies": {
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-regenerator": "^6.26.0"
  }
}

节点mon.json:

{
    "watch": [
        "src",
        "config"
    ],
    "ext": "ts tsx js",
    "exec": "ts-node ./src/server/main.ts"
}

在这种情况下,错误似乎是因为版本ts-node正在使用的不支持 ES 模块语法,正如正在讨论的那样here https://github.com/TypeStrong/ts-node/issues/935#issuecomment-579718641。我从那张票中得知ts-node v8.10.0提供实验支持 https://github.com/TypeStrong/ts-node/issues/1007,所以也许更新它会让这个工作正常。我不知道任何其他解决方案,但您应该能够通过编译来运行文件.ts to .js并运行它们node代替ts-node。否则请告诉我,我很乐意进一步挖掘。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

导入不适用于 tsconfig.json 中的 "module": "ESNEXT" 的相关文章

随机推荐

  • 在单元测试期间模拟 Angular2 中的自定义服务

    我正在尝试为我的服务中使用的组件编写单元测试 组件和服务工作正常 成分 import Component from angular core import PonyService from services import Pony from
  • ExcludeFoldersFromDeployment 在发布配置文件中不起作用

    我尝试使用 ExcludeFoldersFromDeployment 排除发布配置文件中的文件夹 但在发布到 azure app 服务时它不是 wxinclude 文件夹位置 a b c 文件夹名称 如果有人可以帮忙请告诉我 如果您在此环境
  • 有条件 if 对于许多值,更好的方法

    有没有更好的方法来处理检查多个值 当我有超过 3 个选择时 事情就开始变得非常忙碌 if myval something myval other myval third PHP有一个函数叫做in array 像这样使用 in array m
  • C 语言蓝牙编程 - 安全连接和数据传输

    我正在尝试用 C 编写程序通过蓝牙发送 接收数据 我参考了 Albert Huang 写的一本书 里面有示例程序和很好的信息来源 关联 https people csail mit edu albert bluez intro index
  • mysql检查两列之间的预订重叠时间

    我有一个表预订 其中有两列 job time beg 和 job time end 工作从 8 00 开始一直持续到 16 00 现在我需要确定可用时间是否与给定时间重叠 以便我知道我不想在给定时间内预订时间 我在用 select coun
  • 结构体中的枚举; c 新手

    我想知道使用的语法enum in a struct in C 我见过各种例子struct union enum组合用于创建复杂类型 例如 struct MyStruct enum TYPE 1 TYPE 2 TYPE 3 type unio
  • 从int到向量的隐式转换?

    vector
  • 使用访问器方法设置 iVar?

    最初 我正在研究 pickerData 的设置方式 并想知道为什么不能直接分配它 如 METHOD 002 中 但后来我说我应该真正使用我定义的访问器方法而不是直接设置实例变量 我是否正确理解 METHOD 001 是执行此操作的更好方法
  • 从数据库获取提供商信息时发生错误

    得到错误 System Data ProviderInknownException 从数据库获取提供程序信息时发生错误 这可能是由于实体框架使用了不正确的连接字符串造成的 检查内部异常的详细信息并确保连接字符串正确 gt System Da
  • 以编程方式设置 Mac 菜单栏标题

    如何以编程方式更改 Mac 菜单栏中的应用程序标题 after启动应用程序 我知道我可以在运行程序之前通过 Info plist 文件中的 CFBundleName 设置它 但是 我需要在初始化应用程序后更改它 操作我自己的 Info pl
  • 在 NestJS 应用程序中的 Newrelic 中注释匿名中间件

    我在一个项目中使用 NestJS 带有 Express Server 并尝试优化某些端点的性能 使用 New Relic 我注意到所有端点的响应时间的很大一部分都花费在匿名中间件上 达到 89 在某些点上 有没有办法找出这是哪个中间件 我已
  • Myers diff 算法与 Hunt–McIlroy 算法

    最长的公共子序列问题 https en wikipedia org wiki Longest common subsequence problem这是一个经典的计算机科学问题 解决它的算法是版本控制系统和维基引擎的根源 两个基本算法是亨特
  • React-router-dom v4 中的多个嵌套路由

    我需要在react router dom中使用多个嵌套路由 我正在使用react router dom v4 我有我的 import BrowserRouter as Router Route from react router dom 我
  • 使用具有特定布局的 image.plot

    我正在尝试使用 R 函数来设置复杂的图形排列layout包装的graphics x lt y lt seq 4 pi 4 pi len 27 r lt sqrt outer x 2 y 2 当我只使用用该函数创建的图时image 事情按预期
  • 如何设置文本框输入长度

    使用VB6 在我的表单中使用文本框 我想限制文本框中的输入 因此最大值应为 6 用户最多应输入六个字符 否则应显示错误消息 Button1 click if Length textbox1 text gt 6 then enter only
  • 查找依赖于.NET 3.5 SP1的代码

    有没有办法运行某种代码分析来查找可以使用 NET 3 5 SP1 编译但不能使用 3 5 RTM 编译的代码 FxCop 适用于 SP1 引入的程序集 但对于仅调用新方法和属性的代码 它不会检测该用法 当然 您可以使用 fxcop 或 VS
  • 在 iPhone 中标记 CALayers

    我正在寻找一种通用方法 能够在层次结构中搜索唯一的 CALayer 而不必记住该层在层次结构中的位置 并使用 sublayer 和 superlayer 方法 我知道这对于 UIViews 是可能的 这使得翻转视图变得容易 但是 CALay
  • 如何解决此错误 VFY:无法解析虚拟方法

    我正在使用 android studio 2 0 上次我将 jdk 7 升级到 jdk 8 并对文件 gradle 进行了一些更改 但现在我收到此错误 E InstantRun Could not find slices in APK ab
  • 什么是比较合同?

    我的java代码抛出了以下异常 java lang IllegalArgumentException Comparison method violates its general contract 我研究了 StackOverflow 上的
  • 导入不适用于 tsconfig.json 中的 "module": "ESNEXT"

    我在服务器端有以下代码 import as express from express export class AppRouter private static instance express Router static getInsta