Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported #

The error "[ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to be an ESM only package, which means that the package cannot be imported with require() anymore.

error err require esm require of es module

shell
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/borislav/Desktop/node_modules/node-fetch/src/index.js not supported Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules.

You can solve the "[ERR_REQUIRE_ESM]: require() of ES Module not supported" by doing one of two things:

  1. Use ESM - use import foo from 'foo', instead of const foo = require('foo') and add the following line to your package.json file: "type": "module".
package.json
{ "type": "module", // ... "scripts": {}, "dependencies": {}, }
  1. Downgrade to the last version of the package that is built with CommonJS. (Preferred approach if you use TypeScript).
The easiest way to solve the error if using TypeScript is to downgrade the version of the package to one that is built using CommonJS.

You can see which package is causing the error by looking at the error message in your terminal. The error message in the screenshot shows that the node-fetch package is causing the error.

error err require esm require of es module

If any of the following packages are causing the error click on the link to find the fix, otherwise, keep reading:

If you go with using ES Modules, you won't be able to use the require() syntax to import any other packages and you need to update your import statements to ESM.

index.js
// 👇️ default import import fetch from 'node-fetch' // 👇️ named import import {myPackage} from 'my-package'

You also have to add the following line to your package.json file:

package.json
{ "type": "module", // 👇️ ...rest }

You can read more about migrating from CommonJS to ESM by visiting this helpful Github Readme.

At the time of writing, the best solution I've found (especially if using TypeScript) is to downgrade to the most recent version of the package that is built using CommonJS.

For example, the node-fetch package has been converted to be an ESM only package in version 3, so we have to downgrade to version 2.6.7, which is the last version that is built with CommonJS which enables us to use the require() syntax.

To downgrade a package to a specific version, open your terminal in the root directory of your project and run a command that installs the particular version.
shell
npm install node-fetch@2.6.7 # 👇️ NOTE: you only need this if using TypeScript npm install --save-dev @types/node-fetch@2.x

You can use the npm install package@X.X.X syntax to install a specific version of a package.

Once you have installed a version of the package that is built using CommonJS, you will be able to import the package using require().

Check out this GitHub Readme if you want to learn more about why this error occurs and possible fixes.

Another thing to note is you should be using Node.js version >= 14.

You can use the node --version command to check your version of Node.js.

shell
node --version

If you have an older version of Node.js, you can use nvm to install the long-term supported version if you have it installed.

shell
nvm install --lts nvm use --lts

Alternatively, you can install the long-term supported version from the nodejs.org website.

Conclusion #

The error "[ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to be an ESM only package, which means that the package cannot be imported with require() anymore.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.