javascript - "SyntaxError: Cannot use import statement outside of a module"运行编译后的 Javascript 代码时 TypeScript 文件中的错误

标签 javascript node.js typescript syntax-error

我到处寻找这个问题的答案,但没有其他解决方案对我有用。
我收到以下错误:

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/

相关文章:

javascript - Chrome 中的 CSS 延迟加载

node.js - Pug/Jade 插值不适用于属性

javascript - Node.js, 'X' 未定义

javascript - 当在 Node.js 中调用我的函数之一时,程序首先调用 emitHookFactory 并陷入无限循环

arrays - React useState 这里似乎不接受默认值?

javascript - Moment JS比较小时和分钟

附加 HTML 后 JavaScript 不工作

javascript - jquery中动态添加类

javascript - 检查文件是否完全用 node.js 写入

Angular 9 变量未在 View 中更新