Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.

Node.js · updated 2026-11-03

Thrown at startup on Node versions before 20.19 and 22.12 when CommonJS code calls require() on a file or package that is an ES module. Often appears right after bumping a dependency that went ESM-only in a new major version.

/app/src/notify.js:2
const fetch = require('node-fetch');
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/node-fetch/src/index.js from /app/src/notify.js not supported.
Instead change the require of index.js in /app/src/notify.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/app/src/notify.js:2:15) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.11.1

Quick fix

Upgrade Node to 20.19 or 22.12 and later, where require() of a synchronous ES module works without flags. If upgrading is not possible, load the package with await import() inside an async function, pin the last CommonJS major of that dependency, or move your project to ESM with "type": "module".

  1. Run node --version in every environment, including CI and the production image.
  2. Upgrade to Node 22.12+ (or 20.19+) and run the app again.
  3. If the module uses top-level await, require() still fails there; use import() instead.
  4. On older Node, replace require('pkg') with const mod = await import('pkg').
  5. In TypeScript, set module and moduleResolution to nodenext so import() is not compiled to require().
typescript
// CommonJS file on Node < 20.19: load the ESM-only package lazily
async function createQueue() {
  const { default: PQueue } = await import('p-queue');
  return new PQueue({ concurrency: 4 });
}
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/err-require-esm.md and fix this error in this project.

Why it happens

A dependency went ESM-only

Several widely used packages dropped CommonJS in a major release: node-fetch 3, chalk 5, got 12, nanoid 4 and p-queue 7 among them. An unpinned upgrade, or a transitive dependency pulling the new major, breaks every require() of it on older Node.

TypeScript compiles your import into require()

With "module": "commonjs" in tsconfig, TypeScript rewrites import x from 'pkg' into require(), and older setups even turn await import('pkg') into a require call. The ESM package gets required at runtime even though the source says import.

"type": "module" in the nearest package.json

A .js file is treated as ESM when the closest package.json declares "type": "module". Requiring that file from CommonJS, or having a CommonJS tool (older Jest or ESLint setups) load a .js config file there, triggers the error.

Top-level await on newer Node

Even on Node 22.12+ and 20.19+, require() refuses ES modules whose graph uses top-level await and throws ERR_REQUIRE_ASYNC_MODULE instead. Those modules can only be loaded with import().

Production runs an older Node than your laptop

Local development uses a current Node while the Docker base image, serverless runtime or CI image is a release or two behind. The same commit works locally and fails only after deploy.

Other fixes

  • Pin the dependency to its last CommonJS major (for example node-fetch@2) until you can upgrade Node.
  • Convert the project to ESM: add "type": "module" and replace require calls with import statements.
  • For tests, use a runner with native ESM support such as Vitest or node --test, or configure Jest for ESM.
  • Rename a single file to .mjs or .cjs to make its module type explicit regardless of package.json.

Same error, other wording

  • Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /app/node_modules/x/index.js
  • Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/x/index.js from /app/index.js not supported.
  • Instead change the require of index.js in /app/index.js to a dynamic import() which is available in all CommonJS modules.

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the ERR_REQUIRE_ESM errors in production, tell me which package and which require() call trigger them and which Node version that server runs, then either bump the runtime or switch that call to a dynamic import().

Setup for Node.js and Express or Vercel Functions, then have your agent fix and resolve it.

Related errors

More in Imports and modules

Other error topics

Free tools: Source map stack trace decoder · Uptime and SLA downtime calculator · Error budget calculator · Stack trace formatter · JSON log viewer · HTTP status code reference.

References: Node.js modules: CommonJS (loading ES modules with require), Node.js ECMAScript modules, Node.js packages ("type" field)

Spotted a mistake? Email hello@greatwork.company. About Runheed.