繁体   English   中英

不支持 ES 模块的 require()

[英]require() of ES modules not supported

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\...\node_modules\normalize-url\index.js
[0] require() of ES modules is not supported.
[0] require() of D:\...\node_modules\normalize-url\index.js from D:\...\routes\api\users.js 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.
[0] Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\Personnel\Linu
x downloads n docs\Docs\Project 2\node_modules\normalize-url\package.json.

我试图降级 node-fetchand type:module,但没有任何效果。

您安装了一个新版本的normalize-url ,其package.json包含{type:module}这是从:

...package.json 包含“类型”:“模块”

因为模块是使用 'import' 而不是 'require' 加载的,所以 Node 会返回错误。


怎么解决

您可能需要 CJS 脚本。

以标准方式删除并重新安装 package( npm remove normalize-url && npm i normalize-url )。 如果npm i normalize-url@latest已安装,则它是 ES2015 并且仅适用于导入语句。

将节点版本降级到 LTS,它开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM