Skip to main content

Node.js · 4 common causes

ERR_REQUIRE_ESM

Updated

A CommonJS file called require() on a package that ships only as an ES module.

The exact message

/app/index.js:1
const chalk = require('chalk');
              ^

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.
    at Object.<anonymous> (/app/index.js:1:15) {
  code: 'ERR_REQUIRE_ESM'
}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ERR_REQUIRE_ESM.

How to fix it

Cause 1 of 4

The dependency went ESM-only in a recent major — chalk 5, node-fetch 3, got 12, nanoid 4, execa 6 all did.

Load it with a dynamic import instead: const { default: chalk } = await import('chalk'); inside an async function. import() is available from CommonJS and returns a promise; only require() is blocked.

Cause 2 of 4

Your Node version predates require() support for ES modules.

Node 22.12 and the 20.19 backport allow require() of an ES module, as long as that module's dependency graph contains no top-level await. Check with node -v and upgrade before you restructure any code — this often removes the problem entirely.

Cause 3 of 4

The file could be an ES module but the package is not marked as one.

Add "type": "module" to package.json and convert require to import. Do it deliberately: that setting applies to every .js file in the package, so any file that must stay CommonJS needs renaming to .cjs.

Cause 4 of 4

You need this working now and cannot restructure.

Pin the last CommonJS major of the package — npm install chalk@4, npm install node-fetch@2. It works immediately, but it parks you on a line that stops getting fixes, so record it as a stopgap rather than a decision.

Stopping it coming back

Before upgrading a dependency across a major version, check its release notes for an ESM-only switch. Keeping Node on a current LTS also removes the whole class of problem.

ERR_REQUIRE_ESM — common questions

Can I just await an import at the top of a CommonJS file?
No — top-level await only exists in ES modules. Put the dynamic import inside an async function, or restructure so the module exports an async initialiser that the caller awaits.
Why did this start after npm install with no code change?
A caret range let the package move to a new major that dropped CommonJS. npm ls <package> shows the installed version and npm view <package> versions shows what changed. Lockfiles exist to stop this, so check whether the lockfile was regenerated or ignored.
Is converting my project to ESM worth it?
For a new project, yes — the ecosystem is moving that way and tooling now assumes it. For an existing CommonJS codebase with many entry points, the migration touches every file and every test config, so a dynamic import at the one call site is usually the proportionate fix.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor