这个问题以前问过很多次了,但我找不到解决办法。
我在我的项目中使用打字稿。
我有我的index.ts
文件,其中包含如下代码:
import { getEnvOptions } from './env/envreader';
import express, {Express, Request, Response} from 'express';
当我使用命令tsc
编译它并尝试使用node
运行它时
我说错了:
Object.defineProperty(exports, "__esModule", { value: true });
ReferenceError: exports is not defined in ES module scope
据我所知,问题在于节点运行时环境不识别由类型记录编译器生成的关键字"exports“。我打算在服务器上运行这个项目,而不是在浏览器中运行,这就是为什么我将“模块”设置为"CommonJS“。
以下是tsconfig:
{
"compilerOptions": {
"target": "ES5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "CommonJS", /* Specify what module code is generated. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"noEmit": false, /* Disable emitting files from a compilation. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["./**/*.ts"],
}
如果我想继续使用typescript导入语句,那么在节点环境中应该做什么才能使它运行呢?
发布于 2022-10-22 13:27:33
从package.json中删除"type":“模块”,因为您可以使用导入,而无需指定package.json中的导入,原因是导入是类型记录的一部分。
https://stackoverflow.com/questions/74142461
复制相似问题