还有几十个其他问题的标题基本相同,但没有一个答案似乎是相关的,只会增加混乱。
这是我的tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}
这是我输入的内容:
import md5 from 'js-md5';
import got from 'got';
import { Design } from './Design';
...
这是我的错误:
错误 [ERR_REQUIRE_ESM]:必须使用 import 加载 ES 模块:C:\Users\...\node_modules\got\dist\source\index.js
不支持 ES 模块的 require()。
什么。我是not using require
, 我在用着import
。我在任何其他模块上都没有遇到此错误,那么为什么 Got 不同(并且为什么没有什么是简单的)?
为什么会发生这种情况以及如何解决它?
As per @jsejcksn 的回答 https://stackoverflow.com/a/71215905/2288578,我尝试改变我的tsconfig.json
至以下内容:
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}
...并添加了"type": "module"
to my package.json
:
{
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"fs-extra": "^10.0.0",
"got": "^12.0.1",
"js-md5": "^0.7.3",
"moment": "^2.29.1",
"semver": "^7.3.5",
"typescript": "^4.4.3",
"uuid": "^8.3.2",
"@types/node": "^16.9.2",
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.13",
"@types/semver": "^7.3.9",
"@types/uuid": "^8.3.1",
"@types/js-md5": "^0.4.3"
},
"type": "module"
}
但现在我得到了不同的错误:|
src/Logging.ts:1:20 - 错误 TS2792:找不到模块“时刻”。您的意思是将“moduleResolution”选项设置为“node”,还是将别名添加到“paths”选项?
1 个来自“时刻”的导入时刻;
src/SyncAPI.ts:2:17 - 错误 TS2792:找不到模块“got”。您的意思是将“moduleResolution”选项设置为“node”,还是将别名添加到“paths”选项?
2 从“got”导入;