Joe*_*'-' 15 node.js typescript es6-modules
我正在尝试将包 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.
Run Code Online (Sandbox Code Playgroud)
这段代码file.ts
导致了这个问题:
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.
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
import pLimit from 'p-limit';
const limit = pLimit(1);
Run Code Online (Sandbox Code Playgroud)
节点版本:v14.18.0
SNy*_*thi 16
p-limit 4.0.0 及更高版本现在仅适用于 ESM。您可以将 p-limit 降级到 3.1.0,这是 commonjs,它应该可以正常工作。
该软件包现在是纯ESM。请阅读这个。
https://github.com/sindresorhus/p-limit/releases/tag/v4.0.0
或者,您可以将项目从 CJS 切换到 ESM,但这是一个更大的问题。
https://nodejs.org/api/esm.html
归档时间: |
|
查看次数: |
18148 次 |
最近记录: |