Skip to content
Latchkey

Node.js "Must use import to load ES Module" (CJS requiring ESM) in CI

A CommonJS module called require() on a package that is ESM-only. CommonJS cannot synchronously require an ES module, so Node throws and tells you to use import instead.

What this error means

A build or script that uses require() fails after a dependency upgrade. The dependency moved to ESM-only, and the surrounding code is still CommonJS.

node
Error [ERR_REQUIRE_ESM]: require() of ES Module
/work/repo/node_modules/chalk/source/index.js not supported.
Instead change the require of index.js to a dynamic import().
  code: 'ERR_REQUIRE_ESM'

Common causes

A dependency went ESM-only

Popular packages have dropped CommonJS. A major upgrade switches them to ESM, and any require() of them now throws.

Your code is CommonJS

The project compiles or runs as CommonJS, so it uses require. ESM-only deps cannot be required synchronously from that context.

How to fix it

Convert the consumer to ESM

Make the project ESM so it can import the dependency natively.

  1. Add "type": "module" to package.json (or compile to ESM).
  2. Replace require(...) with import ....
  3. Use module/moduleResolution of NodeNext if using TypeScript.

Or pin the last CommonJS version

If converting is not feasible now, pin the dependency to its final CommonJS release.

Terminal
npm install chalk@4

How to prevent it

  • Decide on a module system (ESM) and keep the project consistent.
  • Read major-version changelogs for ESM-only migrations before upgrading.
  • Use import() dynamic import where a CommonJS context must load ESM.

Related guides

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