NodeModulesDependencies

ERR_REQUIRE_ESM: require() of ES Module not supported

Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/some-pkg/index.js not supported.

In short: CommonJS code (require) is loading a package that only ships ES modules (import).

Why it happens

Many packages are now ESM-only. Older Node versions can't require() them at all. Node 22.12+ and 20.19+ support require() of most synchronous ES modules by default, so upgrading Node often fixes it. The reverse error, require is not defined in ES module scope, means a .js file in a "type": "module" package uses require.

How to fix it

  1. Check your Node version: node -v. Upgrade to a current LTS if you're older than the versions above.
  2. Or switch the file to ESM: use import, set "type": "module" in package.json (or rename to .mjs).
  3. If you must stay CommonJS, load the package with dynamic await import('pkg').
  4. Make sure your host builds and runs on the same Node version (set it in the host's settings or engines.node).
Don't: Don't downgrade the package to a years-old version just to keep require().

Copy-paste fix prompt

3 · The fix promptPaste into Cursor, Claude Code, Codex, Lovable, Bolt, Replit or v0
I'm getting this error in my app:

Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/some-pkg/index.js not supported.

[Paste the full error, stack trace, and the file it points to here]

Likely cause: CommonJS code (require) is loading a package that only ships ES modules (import).

Task: Determine whether this project is CommonJS or ESM, which package is ESM-only, and our Node version. Recommend the smallest fix (Node upgrade, ESM switch, or dynamic import) and apply it consistently.

Rules:
- First explain the root cause in 2 sentences and list the files you'll change.
- Make the smallest change that fixes it. Don't refactor unrelated code.
- Don't disable security or checks (RLS, auth, signature verification, type checking, CORS allow-all) to make the error disappear.
- Never print or hard-code secret keys.
- Finish by telling me exactly how to verify the fix.

Common with: Cursor, Claude Code, Replit. Last reviewed 2026-11-13.

Related fixes

← All fixes · Still stuck? Use the diagnose-first prompt