我到处寻找这个问题的答案,但没有其他解决方案对我有用。
我收到以下错误:
D:\some\path\User.model.ts:11
import { Field, ID, InterfaceType, ObjectType } from "type-graphql";
^^^^^^
SyntaxError: Cannot use import statement outside a module
当我尝试运行我编译的 typescript 代码时。我的
package.json
:"scripts": {
"start:dist": "nodemon ./dist/app.js",
"start:dev": "tsnd --respawn --no-notify ./src/app.ts",
"build": "rimraf ./dist && tsc",
"watch": "tsc -w",
"db:setup:test": "tsnd ./src/test-utils/setup",
"test": "npm run db:setup:test && jest"
},
和tsconfig.json
:"target": "ES6",
"module": "commonjs",
我跑npm run watch
在一个终端中,代码以零错误编译。但是当我运行 npm run start:dist
在另一个终端中,它会引发该错误。其他一些帖子提到您需要添加
"type": "module"
至package.json
,但是当我这样做时,它会引发此错误:file://path/to/file/app.js:14
Object.defineProperty(exports, "__esModule", {value: true});
ReferenceError: exports is not defined
谷歌搜索这个错误显示了一些提到删除 "module":"commonjs"
的答案。来自 tsconfig.json
.当我这样做时,它只是提示我必须添加 .js
对于我所有的进口,这似乎是错误的做法。我很不知道该怎么做,所以任何帮助都将不胜感激!
编辑
这是一个带有最小示例的 repo:https://github.com/RiaanLaubscher/Soverflow
最佳答案
我也遇到过这个问题,问题是我有一个 "type"="module"
我的 package.json 文件中的字段,删除该字段后它对我来说很好
关于javascript - "SyntaxError: Cannot use import statement outside of a module"运行编译后的 Javascript 代码时 TypeScript 文件中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64422802/