Skip to content
Latchkey

Node.js ERR_REQUIRE_ESM Deep in the Tree - Fix CJS Requiring an ESM Dep

When a dependency ships as ESM-only, a CommonJS file that require()s it throws ERR_REQUIRE_ESM. This often appears after upgrading a popular utility to a major that dropped its CommonJS build.

What this error means

A CJS codebase crashes with ERR_REQUIRE_ESM after a dependency upgrade, naming a package that is now ESM-only. The require() of that package is no longer supported because it ships only an ES module entry.

Node output
Error [ERR_REQUIRE_ESM]: require() of ES Module
/app/node_modules/chalk/source/index.js from /app/src/log.js not supported.
Instead change the require of index.js to a dynamic import()
    code: 'ERR_REQUIRE_ESM'

Common causes

The dependency became ESM-only

A new major dropped the CommonJS build and ships only ESM. A CJS require() of it is unsupported and throws.

Your code is CommonJS

Because your file uses require, it cannot synchronously load an ESM-only package; ESM must be imported, not required.

How to fix it

Use dynamic import from CJS

Load the ESM-only package via await import() instead of require.

JavaScript
// CJS file
async function main() {
  const { default: chalk } = await import('chalk')
  console.log(chalk.green('ok'))
}

Or pin / migrate deliberately

  1. If you cannot adopt ESM yet, pin the last dependency major that shipped a CJS build.
  2. Plan a migration of the module (or package) to ESM so future upgrades are clean.
  3. Do not mix a top-level require of the ESM-only package back in after pinning.

How to prevent it

  • Check release notes for ESM-only majors before upgrading.
  • Use dynamic import() to consume ESM-only deps from CJS.
  • Plan an ESM migration rather than pinning indefinitely.

Related guides

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