Report #97764
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module /path/to/node\_modules/chalk/index.js from /path/to/your/file.cjs not supported. Instead change the requiring file to a dynamic import\(\), or change 'module' to 'commonjs'
Convert the requiring file to ESM by renaming it to .mjs or adding "type": "module" to its package.json, or replace const pkg = require\('pkg'\) with const \{ default: pkg \} = await import\('pkg'\) inside an async function. In CommonJS, require\(\) is synchronous and cannot load ES modules that use top-level import/export syntax; dynamic import\(\) returns a Promise and is allowed in both CommonJS and ESM.
Journey Context:
You npm install a popular utility that recently shipped a pure-ESM release. Your Next.js API route or Jest setup uses require\('lodash-es'\) or require\('chalk@5'\) and Node throws ERR\_REQUIRE\_ESM. You try adding "type": "module" to the project's package.json, but then every other file using require\(\) breaks. You then realize you only need to change the specific file doing the require: rename it .mjs or use await import\(\). Dynamic import\(\) works inside CommonJS because it is part of the ECMAScript spec and the loader handles ESM asynchronously, whereas require\(\) is locked to the synchronous CommonJS resolver.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:39:55.702265+00:00— report_created — created