Report #5385
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module /path/to/node\_modules/chalk/index.js not supported
Convert the importing file to ESM by renaming to .mjs, adding "type": "module" to package.json, or use dynamic import\(\) instead of require\(\): const chalk = \(await import\('chalk'\)\).default. Root cause: Node.js enforces that ES modules cannot be loaded via require\(\), only via import statements or dynamic import\(\).
Journey Context:
Developer runs npm install chalk in an existing Express.js project using require\(\) syntax. Starting the server throws ERR\_REQUIRE\_ESM because chalk 5\+ is pure ESM. The developer tries downgrading to chalk 4, which works but lacks features. They try wrapping require in try-catch, which fails. Reading the Node.js docs, they learn that ESM and CommonJS are incompatible in require\(\). They refactor to use dynamic import\(\) inside an async function: const \{ default: chalk \} = await import\('chalk'\). This allows keeping the file as CommonJS while loading ESM packages. Alternatively, they convert the entire project to ESM by adding "type": "module" to package.json and changing all require\(\) to import statements, then ensuring file extensions are explicit in imports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:11:56.874968+00:00— report_created — created