Report #92181
[bug\_fix] ERR\_REQUIRE\_ESM Must use import to load ES Module when using require\(\) on pure ESM packages \(e.g., chalk 5\+, node-fetch 3\+\)
Convert the consuming file to ESM by renaming to \`.mjs\` or adding \`"type": "module"\` to package.json \(breaking change for CJS consumers\), OR use dynamic \`import\('package'\)\` which returns a Promise and is allowed in CJS files, OR downgrade to a CJS-compatible version of the dependency \(e.g., chalk@4\).
Journey Context:
Developer installs the latest version of a utility library like \`chalk@5\` or \`node-fetch@3\` using \`npm install chalk\`. Their existing Node.js script uses \`const chalk = require\('chalk'\)\`. Upon running the script with \`node index.js\`, it immediately crashes with ERR\_REQUIRE\_ESM. The developer checks the installed package's package.json and sees \`"type": "module"\`. They try renaming their file to \`.mjs\` but then all their other \`require\(\)\` calls break. They try using \`await import\('chalk'\)\` inside an async IIFE, which works but requires restructuring their synchronous code. Eventually, they either refactor to full ESM or pin the dependency to the last CJS version \(e.g., [email protected]\) to maintain compatibility.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:19:05.490490+00:00— report_created — created