简体   繁体   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.

I tried to downgrade the node-fetchand type:module, nothing worked.我试图降级 node-fetchand type:module,但没有任何效果。

You installed a new version of normalize-url whose package.json contains {type:module} this is deduced from:您安装了一个新版本的normalize-url ,其package.json包含{type:module}这是从:

...package.json contains "type":"module" ...package.json 包含“类型”:“模块”

Because modules are loaded using 'import' and not 'require', Node returns an error.因为模块是使用 'import' 而不是 'require' 加载的,所以 Node 会返回错误。


How to solve怎么解决

You probably need the CJS scripts.您可能需要 CJS 脚本。

Remove and reinstall the package the standard way ( npm remove normalize-url && npm i normalize-url ).以标准方式删除并重新安装 package( npm remove normalize-url && npm i normalize-url )。 If npm i normalize-url@latest is installed then it is a ES2015 and only work with import statements.如果npm i normalize-url@latest已安装,则它是 ES2015 并且仅适用于导入语句。

Downgraded the node version to LTS, and it started working.将节点版本降级到 LTS,它开始工作了。

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

相关问题 错误:导入 node-fetch 时不支持 ES 模块的 require() - Error: require() of ES modules is not supported when importing node-fetch '不支持 ES 模块的 require()。 ' Node.js、快递、swagger-jsdoc 出错 - 'require() of ES modules is not supported. ' error with Node.js, express, swagger-jsdoc 错误:必须使用 import 加载 ES 模块:不支持 ES 模块的 D:\\node_modules\\react-markdown\\index.js require() - Error: Must use import to load ES Module: D:\node_modules\react-markdown\index.js require() of ES modules is not supported 为 ES 模块做 require.resolve - Do require.resolve for ES modules ES 模块中的 require.cache 等价物 - require.cache equivalent in ES modules 无法让 ES 模块与 import 或 require 一起工作 - cant get ES modules to work with import or require 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported 无法从支持的浏览器导入es6模块 - Unable to Import es6 modules from supported browser 相当于 AMD 的用于 ES6 模块的 require.toUrl - Equivalent of AMD's require.toUrl for ES6 modules 带有 axios 的 es6 异步模块 - 参考错误:未定义要求 - es6 async modules with axios - ReferenceError: require is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM