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.1Quick 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".
- Run node --version in every environment, including CI and the production image.
- Upgrade to Node 22.12+ (or 20.19+) and run the app again.
- If the module uses top-level await, require() still fails there; use import() instead.
- On older Node, replace require('pkg') with const mod = await import('pkg').
- In TypeScript, set module and moduleResolution to nodenext so import() is not compiled to require().
// 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 });
}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.
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
- Module not found: Can't resolve 'fs'
- ModuleNotFoundError: No module named 'x'
- [UnhandledPromiseRejection: This error originated either by throwing inside of an async...
More in Imports and modules
- TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/index.tsNode.js
- Error: Cannot find module 'x'Node.js
- SyntaxError: Cannot use import statement outside a moduleNode.js
- Module not found: Can't resolve '@/components/OrderTable'Next.js
- ImportError: cannot import name 'x' from partially initialized module 'y' (most likely...Python
- ImportError: attempted relative import with no known parent packagePython
- AttributeError: module 'requests' has no attribute 'get'Python
- TypeError: 'module' object is not callablePython
- ImportError: libGL.so.1: cannot open shared object file: No such file or directoryPython
- Error: Could not load the "sharp" module using the linux-x64 runtimeNode.js
Other error topics
- Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clientNetwork and HTTP
- Error: connect ETIMEDOUT 10.0.4.12:5432Network and HTTP
- FunctionsHttpError: Edge Function returned a non-2xx status codeNetwork and HTTP
- error: permission denied for table profilesDatabases
- redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection...Databases
- psycopg.errors.UniqueViolation: duplicate key value violates unique constraint...Databases
- Route "/orders/[id]" used `params.id`. `params` should be awaited before using its...React and Next.js rendering
- You're importing a module that depends on `useState` into a React Server Component...React and Next.js rendering
- An error occurred in the Server Components render. The specific message is omitted in...React and Next.js rendering
- UnboundLocalError: cannot access local variable 'total' where it is not associated with a...Undefined, None and type errors
- System.NullReferenceException: Object reference not set to an instance of an object.Undefined, None and type errors
- AttributeError: 'NoneType' object has no attribute 'x'Undefined, None and type errors
- pydantic_core._pydantic_core.ValidationError: 1 validation error for User email Field...JSON, parsing and validation
- SyntaxError: Unexpected end of JSON inputJSON, parsing and validation
- RangeError: Invalid time valueJSON, parsing and validation
- Error: ENOENT: no such file or directory, open '/app/config.json'Memory, async and the process
- django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.Memory, async and the process
- Error: listen EADDRINUSE: address already in use :::3000Memory, async and the process
- Not Acceptable: Client must accept both application/json and text/event-streamMCP servers and clients
- Bad Request: Unsupported protocol version: 2026-07-28MCP servers and clients
- MCP error -32602: Input validation error: Invalid arguments for tool get_orderMCP servers and clients
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.