The Error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”, is an error that is invoked when you are working on node, javascript or nfts. Given below is the snippet of the error you might get:
error [ERR_REQUIRE_ESM]: require() of ES Module [...] is not supported. Instead change the require of index.js [ in my file...] to a dynamic import() which is available in all CommonJS modules
I would like to share with you the steps I took to fix the “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”
Why “error [ERR_REQUIRE_ESM] require() of ES Module […] is not supported” Error is Seen?
The error, “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.” is seen because, of the package ‘node-fetch’. You must downgrade this package to node-fetch@2.6.1 or lower because more recent versions only support ESM.
The error is can also be caused because of the absence of a “type”: “module” in your package.json.
Another very common reason for the error to occur is whenever you install the most recent version of a package that might have problem with importing modules.
The detailed solution to fix the error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”, is given below:
How to fix “error [ERR_REQUIRE_ESM] require() of ES Module […] is not supported” Error?
To fix the error, you have to make sure that you are not using the updated version of node fetch as only recent versions support ESM’s package from version 3.0.0-beta.10. You can install an older version through a simple npm i command. also make sure you have “type”: “module” added in your package.json.
You will also have to check which other updated packages might be causing the error and downgrade it to one down version to make it work and fix error [ERR_REQUIRE_ESM] require() of ES Module […] is not supported.
To fix the error, “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”, you will have to follow one to the methods mentioned below depending on your operating system:
Method 1: Downgrading Node fetch
To fix the error first, you will have to make sure that you are using the downgraded version of node fetch as the recent versions only support ESM’s. You can install an older version through a simple npm i command as mentioned below:
npm i node-fetch@2.6.1
This should fix the error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”.
Method 2: Including type module in package.json
If you still face the error after following the above method, then you will have to make sure to include “type”: “module” in your package.json as mentioned in the code below:
"type": "module",
Note: Rather than writing the following command to import a package: require('chalk')
, in your javascript file you will have to change it to import chalk from 'chalk'
. The example is ‘chalk is used as a reference above.
This should fix the error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”.
Method 3: Checking other Latest Modules
If the error still persists then it very likely that it is be caused due the other package which you have installed in your system or which has recently undergone an update.
You can check which module is causing the error by carefully reading the error and to fix it you would just have to downgrade that particular module.
First uninstall the module completely and then install the specific version again regardless if you are using yarn or npm.
- Remove the
esm
package once. - then Add
"type": "module"
to yourpackage.json
. - Run
node server.js
to start
This should fix the error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”.
Conclusion
To fix the error “error [ERR_REQUIRE_ESM]: require() of ES Module […] is not supported.”, first, you will have to make sure that you are using the downgraded version of node fetch as the recent versions only support ESM’s.
You can install an older version through a simple npm i command. Next make sure you have “type”: “module” added in your package.json.
You will also have to check which other updated package is causing the error and downgrade it.