Report #29456
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module /path/to/module.js from /path/to/script.js not supported
Convert consuming file to ESM by renaming to \`.mjs\`, adding \`"type": "module"\` to package.json, or use dynamic \`import\(\)\` instead of \`require\(\)\` for the ESM-only package. In Node.js 22\+, use \`--experimental-require-module\` flag.
Journey Context:
Developer installs a modern npm package \(e.g., \`chalk@5\`, \`node-fetch@3\`, or \`strip-ansi\`\) and tries to \`require\('chalk'\)\` in a CommonJS \`.js\` file. Node.js throws ERR\_REQUIRE\_ESM. Developer checks the installed package's package.json and sees \`"type": "module"\`, realizing it's an ES Module-only package. Confused because \`require\(\)\` has worked for years, they learn that Node.js treats \`.js\` as CommonJS by default but enforces strict separation: CJS uses \`require/module.exports\`, ESM uses \`import/export\`. They cannot statically \`require\` an ESM module because ESM loads asynchronously and has a different resolution algorithm. The developer converts their entry file to ESM by renaming it to \`.mjs\` or adding \`"type": "module"\` to their package.json, allowing them to use \`import chalk from 'chalk'\`. Alternatively, they refactor the require to use dynamic \`import\('chalk'\)\` inside an async function, which works in CJS files because dynamic import returns a Promise and is the standard bridge between CJS and ESM.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:49:57.365570+00:00— report_created — created