Skip to content
Latchkey

Node "ERR_REQUIRE_ESM: require() of ES Module not supported" in CI

A CommonJS file called require() on a package whose package.json sets "type": "module" or whose entry is .mjs. On Node versions before 22, require() of pure ESM throws ERR_REQUIRE_ESM outright.

What this error means

A require(...) of a dependency (often a recently upgraded one) fails at load time with "Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported", naming the offending package file.

node
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/chalk/source/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.

Common causes

A dependency went ESM-only in a major version

Packages like chalk v5, node-fetch v3, and nanoid v4 dropped CommonJS. A require() of them from a CJS file throws ERR_REQUIRE_ESM.

Your project is CommonJS but imports pure-ESM deps

Without "type": "module", Node treats your .js as CommonJS, so require() is used and cannot load an ESM-only package synchronously on older Node.

How to fix it

Use dynamic import() from CommonJS

A CommonJS file can load an ESM package with await import(...), which returns a promise instead of requiring synchronously.

index.js
// CommonJS file
const chalk = (await import('chalk')).default;
// or inside an async function:
async function main() {
  const { default: chalk } = await import('chalk');
}

Pin the last CommonJS-compatible major

  1. Identify the package named in the error.
  2. Pin it to the last major that still publishes CommonJS (for example chalk@4).
  3. Reinstall and re-run so require() resolves a CJS build.
Terminal
npm install chalk@4

How to prevent it

  • Check a dependency major-bump changelog for "ESM-only" before upgrading.
  • Convert your own package to ESM if most deps have moved.
  • On Node 22+, require() of ESM is allowed but still avoid it for top-level await graphs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →