Hello Guys, How are you all? Hope You all Are Fine. Today I am just added require(‘node-fetch’) and I am facing following error: Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) in nodejs. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
How Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) Error Occurs ?
I am just added require(‘node-fetch’) and I am facing following error.
Error [ERR_REQUIRE_ESM]: require() of ES Module
How To Solve Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) Error ?
How To Solve Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) Error ?
To Solve Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) Erro Here other option you have is to use async
import('node-fetch').then(...)
and Now, Your error will be solved. Or Just Use require (‘cross-fetch’) instead of require(‘node-fetch’). Here is the Documentation cross-fetch.Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’)
To Solve Error [ERR_REQUIRE_ESM]: require() of ES Module Using require(‘node-fetch’) Erro Here is Official Message From node-fetch package: node-fetch is an ESM-only module – you are not able to import it with require. We recommend you stay on v2 which is built with CommonJS unless you use ESM yourself. We will continue to publish critical bug fixes for it. You Can Downgrade to v2 And then You can Use require(‘node-fetch’) and Now Your error must be solved.
Solution 1: downgrade to v2
Here is Official Message From node-fetch package.
node-fetch is an ESM-only module – you are not able to import it with require. We recommend you stay on v2 which is built with CommonJS unless you use ESM yourself. We will continue to publish critical bug fixes for it.
You Can Downgrade to v2 And then You can Use require(‘node-fetch’) and Now Your error must be solved.
Solution 2: Use .then()
Here other option you have is to use async import('node-fetch').then(...)
and Now, Your error will be solved.
Solution 3: Use alternative
Just Use require (‘cross-fetch’) instead of require(‘node-fetch’). Here is the Documentation cross-fetch.
Summary
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read