我想在我的TypeScript项目中导入p-limit包。当我尝试使用tsc && node serve.js
运行项目时,出现以下错误。
我已经被卡了几个小时了...
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /project/node_modules/p-limit/index.js
require() of ES modules is not supported.
require() of /project/node_modules/p-limit/index.js from /project/dist/services/file.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /project/node_modules/p-limit/package.json.
这份代码在 file.ts
中引起了问题:
import pLimit from 'p-limit';
const limit = pLimit(1);
tsconfig.json
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"experimentalDecorators": true,
"types": [
"node",
"express"
],
"strictNullChecks": true
},
"files": [
"./node_modules/@types/node/index.d.ts",
"./node_modules/p-limit/index.d.ts"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Node版本:v14.18.0
"module": "es2017"
。 - gaitat"resolveJsonModule": true
,因为它们是冲突的。在那之后,在构建时,我会收到像这样的错误:node_modules/@types/mongodb/index.d.ts:46:78 - error TS2792: Cannot find module 'bson'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
- Joel'-'