'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.



Solution 1:[1]

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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Minsky
Solution 2 Prajul Aaditya