Report #83101
[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 package.json, and replace require\(\) with import statements. Alternatively, use dynamic import\(\) for the specific ESM package. Root cause: Node.js treats .js as CommonJS by default; ES modules cannot be loaded via require\(\) synchronously.
Journey Context:
Developer starts a new Node.js utility script, npm installs chalk@5 \(pure ESM\). Writes const chalk = require\('chalk'\); in index.js. Running the script immediately throws ERR\_REQUIRE\_ESM pointing to chalk. Developer tries renaming the file to index.mjs, which fixes the chalk import but breaks other internal files that use require\(\). Realizing the project mixes CJS and ESM, developer instead adds "type": "module" to package.json, converting the entire package to ESM. This requires changing all require\(\) to import and module.exports to export. The fix works because it aligns the runtime module system with the package's module type, allowing Node to parse ESM syntax and load chalk's ES module exports correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:04:26.561924+00:00— report_created — created