Report #102420
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module is not supported
If a CommonJS file needs an ESM-only package, replace \`const pkg = require\('pkg'\)\` with \`const \{ named \} = await import\('pkg'\)\`. If the entry file itself must use \`require\(\)\`, rename it to \`.cjs\` so Node treats it as CommonJS regardless of the parent \`package.json\` \`"type": "module"\`. If you control the package, convert to ESM or add a conditional exports map for both CJS and ESM.
Journey Context:
You install a freshly updated dependency such as \`nanoid@5\` or \`node-fetch@3\` in a project that still uses \`const nanoid = require\('nanoid'\)\`. On \`node index.js\` you get \`Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module .../node\_modules/nanoid/index.js from .../index.js not supported\`. The package author has gone ESM-only, but your file is being parsed as CommonJS. You might try renaming \`index.js\` to \`index.mjs\`, but then every \`require\(\)\` in the file breaks. The reliable fix depends on who is the ESM consumer. If only the third-party package is ESM, use dynamic \`import\(\)\` inside your CommonJS file because \`import\(\)\` is valid in both systems and can load ESM. If your own file needs to stay CJS because of \`require\(\)\` calls elsewhere, rename it to \`.cjs\`; the \`.cjs\` extension forces CommonJS mode even under a \`"type": "module"\` package. If you are the package author, publish both CJS and ESM via the \`"exports"\` field so consumers are not forced to rewrite.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:50:56.553894+00:00— report_created — created