我有一个typescript项目,它的编译和运行都很好,但现在每当我尝试运行node时,都会收到错误ReferenceError: require is not defined
。
我不完全确定这是一个Typescript问题,因为当我使用require
运行一个简单的纯js文件时,我会得到同样的错误。我有点困惑,因为在使用node index.js
时应该定义require,不是吗?
tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"noImplicitAny": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist/",
"sourceMap": false
},
"include": ["src/"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
问题是我在package.json
中启用了对ES6模块的实验支持。
从package.json
中删除选项:"type": "module"
解决了我的所有问题。
请参阅此处:官方NODEjs文档上的ECMAScript模块