How to Solve the "ERR_REQUIRE_ESM: require() of ES Module not supported" Error
If you're a Node.js developer, you have likely encountered the Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. This error has become increasingly common as the JavaScript ecosystem transitions from the traditional CommonJS module system (require()) to the modern ECMAScript Modules (ESM) standard (import).
This error occurs when you try to use require() to import a package that is now ESM-only. This guide will explain why this happens and provide you with two clear, practical solutions to fix it.
The Core Problem: CommonJS vs. ES Modules (ESM)
For years, Node.js used the CommonJS module system. You imported packages like this:
// This is CommonJS syntax
const fetch = require('node-fetch');
However, the official standard for JavaScript modules is now ECMAScript Modules (ESM), which uses the import and export syntax.
// This is ESM syntax
import fetch from 'node-fetch';
Many popular open-source packages are migrating to become ESM-only. This means they no longer provide a CommonJS-compatible version. When your project, which is still using require(), tries to import one of these ESM-only packages, Node.js throws the ERR_REQUIRE_ESM error.
Solution 1 (Recommended): Downgrade the Package
For most existing projects (especially those using TypeScript or older build tools), the simplest and most practical solution is to downgrade the problematic package to the last major version that supported CommonJS.
This is a safe and officially recommended approach by many package authors.
The logic:
- Identify the Package: The error message will tell you which package is causing the issue (e.g.,
.../node_modules/node-fetch/src/index.js...). - Find the Last CommonJS Version: Look up the package's documentation or release notes to find the last version before it switched to ESM-only.
- Install the Specific Version: Use
npmoryarnto install that older version.
A possible solution uses node-fetch. The node-fetch package became ESM-only in version 3. The last version that supports require() is version 2.
# Uninstall the current version
npm uninstall node-fetch
# Install the last CommonJS-compatible version
npm install node-fetch@2
After running this, your require('node-fetch') statements will work correctly again.
Solution 2 (Advanced): Migrate Your Project to ESM
The forward-looking solution is to convert your own project to use ESM. This is a more involved process but aligns your project with the modern standard.
The steps:
- Update
package.json: Add the following line to the root of yourpackage.jsonfile. This tells Node.js to treat all.jsfiles in your project as ES Modules.{
"type": "module",
// ... rest of your package.json
} - Convert All
require()toimport: You must go through your entire codebase and change allrequire()statements to theimportsyntax.// Change this:
const myPackage = require('my-package');
// To this:
import myPackage from 'my-package'; - Use Dynamic
import()for CommonJS: If your now-ESM project needs to import an old CommonJS module, you must use a dynamicimport().const myCJSModule = await import('my-old-package');
This is a significant undertaking for an existing project and is often not worth the effort if a simple package downgrade (Solution 1) will suffice.
Common Packages That Cause This Error
This issue is very common with a specific set of popular packages that have recently switched to ESM-only. Here are the packages and the versions to downgrade to:
| Package | Became ESM-only in Version | Downgrade Command |
|---|---|---|
node-fetch | v3 | npm install node-fetch@2 |
chalk | v5 | npm install chalk@4 |
nanoid | v4 | npm install nanoid@3 |
got | v12 | npm install got@11 |
gulp-imagemin | v8 | npm install gulp-imagemin@7 |
Conclusion
The ERR_REQUIRE_ESM error is a sign of the ongoing transition in the Node.js ecosystem. While migrating your own projects to ESM is a good long-term goal, it is often not a practical immediate solution.
- The most effective and recommended solution for most existing projects is to downgrade the problematic package to its last major version that supported CommonJS. This is a low-effort, high-impact fix.
- Migrating your project to ESM is the more "correct" long-term solution but requires a significant refactoring effort.
By identifying the package causing the issue and downgrading it, you can quickly resolve the error and get your project running again.