Report #28845
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module /path/to/node\_modules/chalk/index.js from /path/to/script.js not supported. Instead change the require of index.js to a dynamic import\(\) which is available in all CommonJS modules.
Convert the requiring file to an ES module by renaming it to .mjs or adding "type": "module" to the nearest package.json, and replace require\(\) with import statements. Alternatively, use dynamic import\(\) \(e.g., const chalk = await import\('chalk'\)\) within an async function or CommonJS file. Root cause: Node.js distinguishes CommonJS \(require/module.exports\) and ES Modules \(import/export\); require\(\) cannot load ES modules synchronously.
Journey Context:
Developer writes a script using const chalk = require\('chalk'\) after upgrading chalk to v5 \(which is pure ESM\). Node throws ERR\_REQUIRE\_ESM. Developer tries renaming the file to .mjs, but then all other require\(\) calls in the file break. They read the Node.js error documentation, realizing they need to either convert the entire project to ESM by adding "type": "module" to package.json \(and changing all file extensions or using .cjs for CommonJS files\), or use the dynamic import\(\) function which returns a Promise. They refactor to use \(await import\('chalk'\)\).default inside an async main\(\) function, allowing the script to run without full ESM migration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T02:48:42.076982+00:00— report_created — created