Report #79594
[bug\_fix] ERR\_REQUIRE\_ESM \(ESM vs CommonJS\)
Convert your file to ESM by renaming it to .mjs, or add "type": "module" to your package.json and change all require\(\) to import, or use dynamic import\(\) \(which returns a Promise\) to load the ESM package from CommonJS. Alternatively, downgrade the package to a CommonJS version \(e.g., chalk@4 instead of chalk@5, node-fetch@2 instead of @3\). The root cause is Node.js treating .js files as CommonJS by default; ES Modules cannot be loaded synchronously via require\(\) - they must be parsed as ESM and use the ES module loader, which requires the importing file to be an ES module itself or use the asynchronous import\(\) function.
Journey Context:
You npm install chalk \(v5\+\) or node-fetch \(v3\+\) in your Express app and write const chalk = require\('chalk'\). You start the server and Node immediately crashes with 'Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module not supported'. You check chalk's documentation and see it's now pure ESM. You try renaming your file to .mjs but that breaks all your other require\(\) calls and suddenly \_\_dirname is not defined. You consider converting your entire codebase to ESM by adding "type": "module" to package.json, but that requires changing every require\(\) to import and module.exports to export, and some of your dependencies might not support ESM. You decide on a pragmatic fix: you uninstall chalk@5 and install [email protected] \(the last CJS version\), or you refactor to use dynamic import: const chalk = await import\('chalk'\).then\(m => m.default\); inside an async function. The app starts successfully because you've resolved the module system mismatch.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:11:47.354741+00:00— report_created — created