【发布时间】:2021-11-22 09:24:25
【问题描述】:
我正在尝试将node-fetch 导入我的Typescript 项目,但我遇到了这个错误:
[ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES Module : /Users/xxx/xxx/xxx/xxx/xxx/xxx/xxx/node_modules/node-fetch/src/index.js ES 模块的 require() 是不支持。
这是我的设置:
- 节点:v16.1.0
- 打字稿:4.5.2
node-fetch 包在我的项目中导入如下:import fetch from 'node-fetch';'
这是我的tscongif.json:
{
"compilerOptions": {
"module": "CommonJS",
"sModuleInterop": true,
"target": "ES2020",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"outDir": "dist",
"baseUrl": ".",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"typeRoots": [
"src/@types/",
"node_modules/@types",
],
"strict": true,
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noEmit": true,
"skipLibCheck": true,
},
"include": [
"src/**/*",
"config/**/*"
]
}
我还尝试将"type": "module" 添加到package.json 并将"module": "ES2020" 设置为tsconfig.json,但出现了一个新错误:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts”
要运行我的代码,我使用nodemon 配置如下:
{
"watch": ["src"],
"ext": "ts, js, json",
"execMap": {
"ts": "NODE_ENV=development ts-node -T"
}
}
【问题讨论】:
-
您在哪里看到这些错误?
-
在我的控制台中...使用
nodemon ./src/server.ts,其中nodemon配置为:{ "watch": ["src"], "ext": "ts, js, json", "execMap": { "ts": "NODE_ENV=development ts-node -T" } }。有关更易读的版本,请参阅问题 -
@jsejcksn 对不起,我忘了给你加标签-.-
-
您的问题似乎与存储库中工具的配置有关。您需要提供Minimal, Reproducible Example。在这种情况下,假设想要重现您的问题的人只安装了
node和npm。您需要提供重现问题所需的所有文件和步骤。 -
@jsejcksn 在这里您可以找到一个可重现的示例:github.com/riccardo-nigrelli/node-fetch-problem
标签: node.js typescript