简体   繁体   English

导入不适用于 tsconfig.json 中的“模块”:“ESNEXT”

[英]import doesn't work with “module”: “ESNEXT” in tsconfig.json

I have following code on server side:我在服务器端有以下代码:

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;
  }
}

The VSCODE compiler doesn't show any error on above code. VSCODE 编译器在上述代码上没有显示任何错误。 However, when I run it, It shows following error:但是,当我运行它时,它显示以下错误:

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

SyntaxError: Cannot use import statement outside a module

It worked well when module was set to commonJS in tsconfig.json.当模块在 tsconfig.json 中设置为 commonJS 时,它运行良好。

But for some reason, I need to use ESNEXT for module setting.但由于某种原因,我需要使用 ESNEXT 进行模块设置。

Here is my tsconfig.json:这是我的 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"
  ]
}

How do I solve this?我该如何解决这个问题?

----------- added ------------- - - - - - - 添加 - - - - - - -

package.json: package.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"
  }
}

nodemon.json: nodemon.json:

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

It seems in this case the error shows because the version of ts-node being used does not support ES Module syntax, as is being discussed here .在这种情况下,错误似乎是因为正在使用的ts-node版本不支持 ES Module 语法,正如在此处讨论的那样。 I found out from that ticket that ts-node v8.10.0 provides experimental support , so maybe updating it will get this working.我从那张票中发现ts-node v8.10.0提供了实验性支持,所以也许更新它会让这个工作正常。 I don't know of any other solutions, but you should be able to run the files by compiling the .ts to .js and run them with node instead of ts-node .我不知道任何其他解决方案,但您应该能够通过将.ts编译为.js并使用node而不是ts-node来运行文件。 Let me know otherwise, I'm happy to dig a little further.否则请告诉我,我很乐意进一步挖掘。

This worked for me.这对我有用。 Try running尝试运行

ts-node --transpile-only --script-mode file_name.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 tsconfig.json 的路径在 tsc 之后不起作用 - paths from tsconfig.json doesn't work after tsc 当我将 tsconfig.json 中的“模块”更改为“ESNext”并将 package.json 中的“类型”更改为“模块”时,出现错误 - When I change "module" in tsconfig.json to 'ESNext' and "type" in package.json to 'module', I get an error tsconfig.json 中模块类型的区别 - Difference between module type in tsconfig.json “语法错误:不能在模块外使用导入语句”但 tsconfig.json `module` 已经 `commonjs` - "SyntaxError: Cannot use import statement outside a module" but tsconfig.json `module` already `commonjs` 找不到模块,因为 tsconfig.json 中提到了路径 - cannot find module since the path is mentioned in the tsconfig.json 在 tsconfig.json 中设置 "type": "module" 后开玩笑停止工作 - jest stop working after setting "type": "module" in tsconfig.json “找不到模块 'react'”和“找不到 tsconfig.json” - “Cannot find module 'react'” and “tsconfig.json not found” 打字稿错误在 tsconfig.json 中将模块更改为“umd”或“amd”时找不到模块“typescript-Collections” - Typescript error Cannot find module 'typescript-Collections' when changing the module to “umd” or “amd” in tsconfig.json tsc忽略了​​我的tsconfig.json文件 - tsc ignores my tsconfig.json file tsconfig.json中文件夹中的相对文件路径 - Relative file path in folders in tsconfig.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM