Node "Must use import to load ES Module" - Fix require() of ESM in CI
This error means CommonJS code used require() on a package that ships only as ESM. On older Node, ESM cannot be loaded with require, so the call throws ERR_REQUIRE_ESM. It usually appears after a dependency went ESM-only.
What this error means
A require(...) of a dependency throws Error [ERR_REQUIRE_ESM]: Must use import to load ES Module. It typically follows upgrading a package (e.g. a utility library) to a major that dropped CommonJS.
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 to a dynamic import() which is
available in all CommonJS modules.Common causes
A dependency became ESM-only
A package upgraded to a version that publishes only ESM. CommonJS require() of it fails on Node versions that do not allow requiring ESM.
Your code is CommonJS but consumes ESM-only deps
A CJS codebase that pulls in ESM-only libraries must use dynamic import() or move to ESM; static require will not work.
How to fix it
Use dynamic import or pin a CJS version
Load the ESM module with await import() from CommonJS, or pin the last CJS-compatible version.
// CommonJS calling an ESM-only package
const chalk = (await import('chalk')).default;
// or pin the last CJS major:
// npm install chalk@4Move the project to ESM
- Set
"type": "module"and convertrequire/module.exportstoimport/export. - Or pin ESM-only dependencies to their last CJS release as a stopgap.
- Upgrade Node so newer
require(esm)support is available where applicable.
How to prevent it
- Track when dependencies go ESM-only and plan the migration.
- Use dynamic
import()from CommonJS for ESM-only packages. - Standardize on one module system for your package.