繁体   English   中英

TypeScript & NodeJS:ES 模块中未定义导出 scope

[英]TypeScript & NodeJS: Exports is not defined in ES module scope

我用导入/导出语法编写了我的 NodeJS JavaScript 文件,我的 package.json 有"type": "module"

问题是tsc构建代码后,/build/ /build/server.js中的转译代码无法运行。 它说Exports is not defined in ES module scope

这是我的tsconfig.json

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "incremental": true,
    "target": "es2019" ,
    "module": : "commonjs",
    "allowJs": true ,
    "outDir": "./build",
    "strict": true ,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true ,
    "alwaysStrict": true ,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true ,
    "inlineSourceMap": true ,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true ,
    "forceConsistentCasingInFileNames": true,
    "useUnknownInCatchVariables": false
  }
}

如果我删除"module": "commonjs"并添加"moduleResolution": "Node" ,我的导入会出现不同的错误:

找不到从 /build/server.js 导入的模块 /build/constants模块 /build/constants.js 确实存在,但在我的代码中我没有将 .js 添加到我的导入中,所以我像这样import {CONSTANTS} from 'constants'import {CONSTANTS} from 'constants'

在 TypeScript 文件中,当导入其他 TypeScript 文件时,导入应该像import {CONSTANTS} from constants.jsimport {CONSTANTS} from constants.tsimport {CONSTANTS} from constants ,这就是我现在拥有的。

我是 TypeScript 的新用户。解决我遇到的这个问题的最佳方法是什么?

谢谢

将 tsconfig“module”和“target”更新为“ES6”解决了这个问题。

我找到了解决方案。 "type": "module"必须从 package.json 中删除

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM