Lorixen.
EN

require() of ES Module not supported? Fix ERR_REQUIRE_ESM

Short answer: ERR_REQUIRE_ESM means your code used require() to load a package that is now an ES module, and the Node.js version running it can't do that. It often appears after npm update, when a library you depend on releases a new major version that drops CommonJS. On current Node.js releases, require() can load most ES modules directly, so the quickest fix is often upgrading Node. Otherwise, load the package with import(), move your own code to ES modules, or pin the last version of the package that still supports require().

Two module systems

Node.js has two ways of organising code:

  • CommonJS, the original system, using require() and module.exports.
  • ES modules (ESM), the JavaScript standard, using import and export.

Node decides which a file is by its extension and its package settings. Files ending in .mjs are ES modules. A .js file is treated as ESM if the nearest package.json has "type": "module", and Node can also detect ES module syntax in .js files when no "type" is set.

Many popular libraries have moved to ESM-only releases. If your project is still CommonJS, upgrading one of them can break code that used to require() it.

Why the error depends on your Node version

Node's documentation shows that loading ES modules with require() was added in v22.0.0 and v20.17.0 behind a flag, and then enabled by default in v23.0.0, v22.12.0 and v20.19.0. It later stopped printing an experimental warning, and the docs mark it as no longer experimental from v25.4.0.

The error list in the same documentation now marks ERR_REQUIRE_ESM as deprecated, because require() supports loading synchronous ES modules. In practice that means:

  • On older Node versions, such as anything before 20.19 or 22.12, require() of an ES module throws ERR_REQUIRE_ESM.
  • On newer versions, the same code usually works.

That's why a script can work on a developer's laptop and fail on a server, a CI runner or a hosting platform still pinned to an older Node release.

Fix 1: upgrade Node.js where the code runs

Check node --version everywhere the code runs: laptops, CI, servers, serverless functions and scheduled jobs. Moving to a supported release where require(esm) is enabled by default fixes many cases with no code change.

Keep the version consistent with an .nvmrc file, the engines field in package.json, or your hosting platform's runtime setting, so the next deploy doesn't silently use an older one.

Fix 2: the "async module" variant

Even on new versions, require() only works for ES modules that are fully synchronous. Node's documentation says that if the module uses top-level await, Node throws ERR_REQUIRE_ASYNC_MODULE instead, and you should load it with import().

Dynamic import() works in CommonJS files and returns a promise:

async function main() {
  const { default: fetchOrders } = await import('some-esm-package');
  // use it here
}
main();

The awkward part is that everything using the package now has to be async, which can ripple through a small script.

Fix 3: move your project to ES modules

For code you maintain long term, switching to ESM is often the cleanest path:

  1. Set "type": "module" in package.json, or rename entry files to .mjs.
  2. Replace require() with import and module.exports with export.
  3. Replace __dirname and __filename, which don't exist in ES modules, with equivalents based on import.meta.
  4. Add file extensions to relative imports, which ESM requires.

Test thoroughly, especially config files for build tools, because some tools look for specific file names or extensions. Convert one entry point at a time if the project is large, and run your test suite after each step.

Fix 4: pin the previous version

If you need the system working today, pin the dependency to the last version that supported CommonJS in package.json and commit the lock file. Make a note to revisit it, because older versions stop receiving fixes, including security fixes.

When the namespace looks odd

When require() loads an ES module on newer Node, it returns the module's namespace object. If the package uses a default export, you may need require('pkg').default rather than require('pkg'). Node's documentation also mentions an __esModule marker added for compatibility with tools that convert ES modules to CommonJS; code written directly in CommonJS shouldn't rely on it.

For troubleshooting, Node provides --trace-require-module to print where require() loads ES modules, and --no-require-module to turn the feature off if it causes unexpected breakage. You can check process.features.require_module to see whether the running Node supports it.

TypeScript and bundlers

If you write TypeScript or use a bundler, your build settings decide what the output looks like. A project compiled to CommonJS can produce require() calls for packages that are ESM-only. Check your compiler's module settings and your bundler's output format along with the Node version the result runs on.

An illustrative example

A web agency in Leeds runs a Node script every night that pulls orders from a client's shop and posts a summary to their chat. After a routine npm update, the job fails with ERR_REQUIRE_ESM. The developer's laptop runs a recent Node release, but the server runs an older one. They upgrade Node on the server, add the version to .nvmrc and the engines field, and add a CI check that runs the script on the same version as production.

FAQ

Should I delete node_modules and reinstall?

That rarely fixes this error. The problem is how the package is loaded, not a corrupted install.

Can I mix CommonJS and ES modules in one project?

Yes, using .cjs and .mjs extensions or package settings, but keep the boundaries clear.

Why did it break without me changing code?

A dependency update or a different Node version on the server changed what's loaded, even though your files didn't change.

Is ESM faster than CommonJS?

That isn't the reason to switch. The reason is compatibility with packages that are now ESM-only.

If a Node.js script or app broke after an update and you want it fixed and kept stable, tell us what it runs on.

Sources

WhatsAppBook a call