Report #13134
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the importing file to ESM by renaming it to .mjs or adding 'type': 'module' to the nearest package.json, and replace require\(\) with import; or alternatively, use dynamic import\('chalk'\) which returns a Promise and works in CommonJS; or downgrade the dependency to a CommonJS-compatible version \(e.g., chalk ^4.1.2 instead of ^5.0.0\).
Journey Context:
You run npm update which upgrades chalk to version 5.0.0. Your existing CommonJS script const chalk = require\('chalk'\); immediately throws ERR\_REQUIRE\_ESM. You try changing it to import chalk from 'chalk'; but that throws SyntaxError: Cannot use import statement outside a module. You consider renaming the file to .mjs, but that breaks other require\(\) calls in your project. Reading the Node.js ESM documentation, you realize you can use dynamic import: const chalk = \(await import\('chalk'\)\).default; \(or import\('chalk'\).then\(...\)\) within an async function, which allows you to consume the ESM package from CommonJS code. Alternatively, you pin chalk to ^4.1.2 which remains CommonJS until you can migrate the entire project to 'type': 'module'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T17:49:28.786027+00:00— report_created — created