How to fix node-fetch Error ERR_REQUIRE_ESM
If you are using node-fetch
in your nodejs project you might will get this error Error [ERR_REQUIRE_ESM]: require() of ES Module
.
To fix this issue it is because you are using the new version or above 3.x
. And according to the node-fetch it will require Node.js version above 12.20.0
that use module to load the package.
How to fix it?
So for that you need to import it like this.
import fetch from 'node-fetch';
And on package.json
set the project type to support module like this.
"type": "module"
But if you don't want to use the new version of node-fetch you can install the previous version 2.x
to install it you can run this command.
npm install node-fetch@2
or if you are using yarn
you can do it like this.
yarn add node-fetch@2
And that is hopefully this will help fix your problem, let me know if you have any question, feel free to send me a message via twitter.