Report #9840
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the importing file to ESM by renaming it to .mjs, adding '"type": "module"' to package.json, or replace the require\(\) call with a dynamic import\(\) which returns a Promise and is supported in CommonJS modules.
Journey Context:
Developer npm installs chalk@5 or node-fetch@3 in an existing CommonJS project \(const chalk = require\('chalk'\)\), immediately crashes with ERR\_REQUIRE\_ESM on startup. Checking the package.json of the dependency reveals '"type": "module"'. Attempting to rename the consuming file to .mjs breaks other require\(\) calls in the codebase. Researching Node.js ESM interop docs reveals that dynamic import\(\) is valid inside CommonJS and loads ESM modules asynchronously. Refactoring to const \{default: chalk\} = await import\('chalk'\) resolves the error without a full ESM migration, unblocking the upgrade path.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:14:33.243609+00:00— report_created — created