I am trying to run my nodejs app but I am facing the following error: [ERR_REQUIRE_ESM]: require() of ES Module from not supported. In this Exerror article, We are going to learn about How to reproduce this error and we will discuss All Possible Solutions Let’s Get Start with This Article.
How [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error Occurs?
I am trying to run my nodejs app but I am facing the following error.
[ERR_REQUIRE_ESM]: require() of ES Module from not supported
So here I am writing all the possible solutions that I have tried to resolve this error.
How To Solve [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error?
- How To Solve [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error?
To Solve [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error Just After node-fetch version 3 is Released they are stopped supporting require() in importing. So You can Downgarde node-fetch to 2.6.1or 2.6.6 which enables us to use the
require()
syntax and your error will be resolved. First of all open your terminal and then runn this command to downgrade node-fetch version: npm install node-fetch@2.6.6 and then You are able to use require() while importing something without facing any error. thanks. - [ERR_REQUIRE_ESM]: require() of ES Module from not supported
If you are using
node-fetch
v3 then node-fetch is stopped supporting require() word in importing. If You want to import anything thing then you need to use import something from something. To Solve [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error You Just need to use import instead of require() Just like this: import fetch from “node-fetch”; And Also You need to add “type”: “module” in your package.json file Just like this: “type”: “module”, And Now, Your error will be solved.
Solution 1: use import instead of require()
If you are using node-fetch
v3 then node-fetch is stopped supporting require() word in importing. If You want to import anything thing then you need to use import something from something. To Solve [ERR_REQUIRE_ESM]: require() of ES Module from not supported Error You Just need to use import instead of require() Just like this.
import fetch from "node-fetch";
And Also You need to add “type”: “module” in your package.json file Just like this.
{
....
"type": "module",
....
}
And Now, Your error will be solved.
Solution 2: Downgrade node-fetch
Just After node-fetch version 3 is Released they are stopped supporting require() in importing. So You can Downgarde node-fetch to 2.6.1or 2.6.6 which enables us to use the require()
syntax and your error will be resolved.
First of all open your terminal and then runn this command to downgrade node-fetch version.
npm install node-fetch@2.6.6
OR
npm install node-fetch@2.6.61
and then You are able to use require() while importing something without facing any error. thanks.
Conclusion
It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
- Impish Release no longer has a Release file
- How to Check if String Contains a word in Javascript?
- ModuleNotFoundError: No module named ‘requests’
- ValueError: I/O operation on closed file in Python
- An unhandled exception occurred: catch clause variable is not an Error instance