Report #66844
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module
For ESM packages in a CJS project, use dynamic import\(\): const \{default: chalk\} = await import\('chalk'\); Note this returns a Promise, so it must be used in an async function. Alternatively, convert your entire project to ESM by adding 'type': 'module' to package.json, changing all require\(\) to import, and using import.meta.url instead of \_\_dirname. Or, simply downgrade to the CJS version of the package \(e.g., chalk@4 instead of chalk@5\).
Journey Context:
You have a TypeScript project with tsconfig.json set to 'module': 'commonjs'. You npm install chalk@5 and import it with import chalk from 'chalk'. TypeScript compiles this to const chalk = require\('chalk'\). When you run node dist/index.js, it crashes with ERR\_REQUIRE\_ESM, stating that chalk is an ES module and cannot be required\(\). You try changing tsconfig to 'module': 'ES2020', but then Node crashes because it sees require\(\) calls for other CJS modules and can't handle the mix. The rabbit hole involves understanding that once a file is treated as ESM \(via 'type': 'module' or .mjs\), Node forbids require\(\) entirely, and CJS files cannot synchronously require\(\) ESM files.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T18:40:38.700878+00:00— report_created — created