Node.js Error ERR_REQUIRE_ESM: require() of ES Module not supported
CommonJS code called require() on an ESM-only package (node-fetch 3, chalk 5, nanoid 4…). Use await import(), switch your project to ESM, pin the last CJS version, or upgrade to Node ≥20.19 / 22.12 where require(esm) works.
Identify: does your log match?
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/node-fetch/src/index.js from /app/index.js not supported.
Instead change the require of /app/node_modules/node-fetch/src/index.js in /app/index.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/app/index.js:1:15) {
code: 'ERR_REQUIRE_ESM'
}- High
Dependency went ESM-only
A major version bump (e.g. node-fetch 2 → 3) removed the CommonJS build. Your require() call is unchanged.
- High
TypeScript compiles imports to require
"module": "commonjs" in tsconfig turns import x from 'esm-pkg' into require(), which fails at runtime.
- Medium
Old Node version
Node before 20.19 / 22.12 cannot require() ES modules at all. Newer versions only fail for modules with top-level await (ERR_REQUIRE_ASYNC_MODULE).
Fix
Use dynamic import() from CommonJS
const fetch = require('node-fetch');
async function getJson(url) { const { default: fetch } = await import('node-fetch'); const res = await fetch(url); return res.json(); } // or drop the dependency: global fetch is built into Node 18+
import() is allowed in CJS and loads ESM asynchronously. On Node 18+, the built-in fetch removes the need for node-fetch.
Verify: reproduce it on purpose
$ cd "$(mktemp -d)" && npm i -s node-fetch@3 && npx -y node@18 -e "require('node-fetch')"Run it against a dev environment to trigger ERR_REQUIRE_ESM on purpose and test your error handler.
Prevent
- Check a package's "exports" and "type" before major upgrades.
- Prefer ESM for new Node projects.
- Upgrade to Node 22 LTS for require(esm).
FAQ
Does Node 22 still throw ERR_REQUIRE_ESM?
From 22.12 (and 20.19) require() of synchronous ES modules is enabled by default. Modules with top-level await throw ERR_REQUIRE_ASYNC_MODULE instead.
Can I just downgrade the package?
Yes: node-fetch@2, chalk@4 and nanoid@3 still ship CommonJS. It is a quick fix but you miss future updates.
Why does Jest throw this but node does not?
Jest uses its own CJS module system. Configure transformIgnorePatterns to transpile the ESM package, or use Vitest.
Related errors
Last verified 2026-10-21 · Source: POSIX errno + Node.js ERR_* codes (doc/api/errors.md) · Official docs ↗ · ~18,100 searches/mo