ERR_REQUIRE_ESM: What It Means and How to Fix It
Error reference
ERR_REQUIRE_ESM
Language
Severity
HighWhen it happens
Node.js throws ERR_REQUIRE_ESM when a CommonJS require() call tries to load an ES Module.
Potential fixes
Replace require() with dynamic import(): const mod = await import('pkg'). Alternatively, pin to the last CommonJS version of the package (e.g. chalk@4). For greenfield projects, migrate fully to ESM by adding 'type': 'module' to package.json.
Deep dive
Node.js throws ERR_REQUIRE_ESM when a CommonJS require() call tries to load an ES Module. CommonJS and ES Modules have different loading mechanisms, and Node.js does not allow synchronous require() to load async ES Modules.
Why it happens
- •A popular package (chalk, node-fetch, etc.) released a new major version as ESM-only.
- •A .mjs file or a package with 'type': 'module' is required from a CommonJS module.
- •Migrating a project to ESM partway and mixing require() and import.
Potential fixes
Replace require() with dynamic import(): const mod = await import('pkg'). Alternatively, pin to the last CommonJS version of the package (e.g. chalk@4). For greenfield projects, migrate fully to ESM by adding 'type': 'module' to package.json.
Examples
Code that triggers the error
// In a CommonJS file (.js without "type":"module")
const chalk = require("chalk"); // chalk v5+ is ESM-onlyError output
Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/chalk/source/index.js not supported. Instead change the require of index.js in ... to a dynamic import() which is available in all CommonJS modules.
Fixed code
// Option 1: use dynamic import
const chalk = await import("chalk").then(m => m.default);
// Option 2: downgrade to a CommonJS version (e.g. chalk@4)
// npm install chalk@4Practice in English
How would you explain an ERR_REQUIRE_ESM to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free