Report #26295
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the requiring file to ESM by renaming to .mjs, adding 'type': 'module' to package.json, or use dynamic import\(\) instead of require\(\) for the ESM package
Journey Context:
You upgrade a dependency \(like chalk 5\+, got 12\+, or node-fetch 3\+\) and suddenly your CommonJS app crashes on startup with ERR\_REQUIRE\_ESM. You check the package.json of the dependency and see 'type': 'module'. You try requiring it with require\(\) but Node.js throws because it's an ES Module. You consider downgrading but need the new features. The rabbit hole involves understanding Node.js module systems: require\(\) is synchronous and CJS, while ESM uses import which is async-capable and statically analyzed. When a package is pure ESM, it cannot be require\(\)d. The fix works because converting your entry point to .mjs or setting type: module makes Node treat your code as ESM allowing static import statements, or using dynamic import\(\) returns a Promise that loads the ESM module asynchronously at runtime, bypassing the require\(\) restriction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T22:32:09.155406+00:00— report_created — created