简体   繁体   中英

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.

You installed a new version of normalize-url whose package.json contains {type:module} this is deduced from:

...package.json contains "type":"module"

Because modules are loaded using 'import' and not 'require', Node returns an error.


How to solve

You probably need the CJS scripts.

Remove and reinstall the package the standard way ( 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.

Downgraded the node version to LTS, and it started working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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