admin管理员组文章数量:1245641
I have written my NodeJS JavaScript files with Import/Export syntax and my package.json has "type" : "module"
The problem is that after tsc
builds the code, the transpiled code in /build/server.js
cannot run. It says Exports is not defined in ES module scope
Here's my tsconfig.json
{
"pilerOptions": {
"resolveJsonModule": true,
"incremental": true,
"target": "es2019" ,
"module": : "monjs",
"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
}
}
if I remove "module" : "monjs"
and add "moduleResolution": "Node"
, I get a different error with my imports:
Cannot find module /build/constants imported from /build/server.js
The module /build/constants.js does exist, but in my code I'm not adding .js to my imports, so I'm importing it like this import {CONSTANTS} from 'constants'
.
In a TypeScript file, when importing other TypeScript files, should the imports be like import {CONSTANTS} from constants.js
or import {CONSTANTS} from constants.ts
or import {CONSTANTS} from constants
, which is what I have now.
I'm new to TypeScript. What's the best way to solve this problem I'm having?
Thanks
I have written my NodeJS JavaScript files with Import/Export syntax and my package.json has "type" : "module"
The problem is that after tsc
builds the code, the transpiled code in /build/server.js
cannot run. It says Exports is not defined in ES module scope
Here's my tsconfig.json
{
"pilerOptions": {
"resolveJsonModule": true,
"incremental": true,
"target": "es2019" ,
"module": : "monjs",
"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
}
}
if I remove "module" : "monjs"
and add "moduleResolution": "Node"
, I get a different error with my imports:
Cannot find module /build/constants imported from /build/server.js
The module /build/constants.js does exist, but in my code I'm not adding .js to my imports, so I'm importing it like this import {CONSTANTS} from 'constants'
.
In a TypeScript file, when importing other TypeScript files, should the imports be like import {CONSTANTS} from constants.js
or import {CONSTANTS} from constants.ts
or import {CONSTANTS} from constants
, which is what I have now.
I'm new to TypeScript. What's the best way to solve this problem I'm having?
Thanks
Share Improve this question asked Feb 2, 2022 at 11:19 RoozRooz 8241 gold badge10 silver badges21 bronze badges2 Answers
Reset to default 13I found the solution. "type" : "module"
had to be removed from package.json
Updating tsconfig "module" and "target" to "ES6" solved the issue.
本文标签: javascriptTypeScript amp NodeJS Exports is not defined in ES module scopeStack Overflow
版权声明:本文标题:javascript - TypeScript & NodeJS: Exports is not defined in ES module scope - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740227534a2244967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论