Report #104387
[bug\_fix] ERR\_REQUIRE\_ESM: require\(\) of ES Module from CommonJS
Convert your \`require\(\)\` call to a dynamic \`import\(\)\` or change the importing script to ESM \(set \`"type": "module"\` in package.json or rename file to \`.mjs\`\). Example: change \`const pkg = require\('esm-only-package'\)\` to \`const pkg = await import\('esm-only-package'\)\`.
Journey Context:
A developer had a CommonJS build script \(\`build.js\`\) that required \`chalk\` v5 \(which is ESM-only\). Running \`node build.js\` threw \`ERR\_REQUIRE\_ESM\`. They initially tried to downgrade \`chalk\` to v4 \(CJS\) but that broke other newer dependencies. The correct fix was to convert the build script to ESM: rename to \`build.mjs\` and use \`import chalk from 'chalk'\`. Alternatively, they could use \`const chalk = await import\('chalk'\)\` inside an async function in the CommonJS file. The root cause: since Node 12, packages can specify ESM-only exports, and \`require\(\)\` cannot load them because ESM modules have different resolution and execution rules. The dynamic import works because it returns a promise that resolves to the module namespace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-09T20:06:01.469091+00:00— report_created — created