Lingua-e
JavaScript

What is a ERR_REQUIRE_ESM?

← Errors

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.

Examples

JavaScript

Code that triggers the error

// In a CommonJS file (.js without "type":"module")
const chalk = require("chalk"); // chalk v5+ is ESM-only

Error 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.

How to fix it

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.

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@4

Related errors

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