Report #62334
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
Convert the consuming file to ESM by renaming it to .mjs, or add "type": "module" to package.json and convert all require\(\) to import, or use dynamic import\(\) instead of require\(\). Root cause: Node.js strictly enforces that ES Modules cannot be loaded via the synchronous require\(\) used by CommonJS.
Journey Context:
You npm install chalk@5 \(or got@12, or node-fetch@3\) and try to require it in your index.js with const chalk = require\('chalk'\). You immediately get ERR\_REQUIRE\_ESM. You check the package.json of chalk and see it has "type": "module", meaning it's pure ESM. You try using createRequire but that doesn't work for true ESM. You consider downgrading to chalk@4 which is CommonJS, but you want the new features. The real solution is to embrace ESM: you rename your file to index.mjs or add "type": "module" to your package.json and change all require\(\) to import statements. If you can't convert everything yet, you use dynamic import\('chalk'\) which returns a Promise and works inside CommonJS files. This fixes it because Node.js treats ESM as a distinct module system with static analysis that cannot be synchronously required.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:06:54.704591+00:00— report_created — created