Last updated: Apr 4, 2024
Reading timeยท4 min

The error "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module" occurs for 2 main reasons:
type to module in your package.json but use the CommonJS
require() syntax in your code.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:
type to module in your package.json file and use the import and
export ES Modules syntax.{ "type": "module", // ... "scripts": {}, }

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:
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.
// ๐๏ธ 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));

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:
{ "type": "module", // ๐๏ธ ...rest }
Here is an example of a default and named export.
// ๐๏ธ 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 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.
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.
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.
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.
# ๐๏ธ (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.
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.
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.
nvm install --lts nvm use --lts
Alternatively, you can install the long-term supported version from the nodejs.org website.
The error occurs for 2 main reasons:
type to module in your package.json but use the CommonJS
require syntax in your code.