Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

The error "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module" occurs for 2 main reasons:

  1. A module you are importing has been converted to an ESM-only module.
  2. You have set type to module in your package.json but use the CommonJS require() syntax in your code.
shell
internal/modules/cjs/loader.js:821 throw new ERR_REQUIRE_ESM(filename); Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/bobbyhadz/dynamic-es6-mod/src/index.mjs at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:821:9) at Module.load (internal/modules/cjs/loader.js:643:32) at Function.Module._load (internal/modules/cjs/loader.js:556:12) at Function.Module.runMain (internal/modules/cjs/loader.js:839:10) at internal/main/run_main_module.js:17:11

You can solve the "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module" by doing one of two things:

  1. Set type to module in your package.json file and use the import and export ES Modules syntax.
package.json
{ "type": "module", // ... "scripts": {}, }

set type property to module in package json

  1. Downgrade the version of the package that caused the issue to the last version that was built using CommonJS.
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.

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

If you decided to use ES Modules and the import/export syntax, you won't be able to use require() in import statements anymore.

You need to update all your import and export statements to the following.

index.js
// ๐Ÿ‘‡๏ธ a default import import _ from 'lodash'; console.log(_.uniq([1, 1, 3])); // --------------------------- // ๐Ÿ‘‡๏ธ a named import import {multiply} from './another-file.js'; console.log(multiply(50, 50));

update all your import and export statements

Notice that we had to specify the extension when importing from a local module.

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

package.json
{ "type": "module", // ๐Ÿ‘‡๏ธ ...rest }

Here is an example of a default and named export.

another-file.js
// ๐Ÿ‘‡๏ธ a default export export default function sum(a, b) { return a + b; } // ๐Ÿ‘‡๏ธ a named export export const multiply = (a, b) => { return a * b; };

Once you set type to module in package.json, make sure to replace all occurrences of require() with import/export statements.

# If the error is caused by a third-party package, downgrade its version

If you get the error when using a third-party package, it's best to downgrade its version to the last version that was built using CommonsJS, especially when using TypeScript.

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.

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

The error also occurs when you have a different version of Node.js than the one that was used to install the packages in your project.

# Delete your node_modules and package-lock.json files and run npm install

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If the error persists, try to upgrade Node.js to the long-term supported version.

# Make sure your Node.js version is up to date

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 occurs for 2 main reasons:

  1. A module you are importing has been converted to an ESM-only module.
  2. You have set type to module in your package.json but use the CommonJS require syntax in your code.
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.

Copyright ยฉ 2024 Borislav Hadzhiev