Report #51036
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... not supported
Refactor the importing code to use dynamic \`import\(\)\` instead of \`require\(\)\`, or convert the file to an ES Module by renaming it to \`.mjs\` or adding \`"type": "module"\` to package.json. Root cause: Node.js enforces strict separation between CommonJS \(synchronous require\) and ES Modules \(asynchronous import\); ESM files cannot be loaded via require\(\) since ESM has a different loading phase and static analysis requirements.
Journey Context:
Developer installs the latest version of \`node-fetch\` \(v3\+\) in an existing CommonJS Express app using \`require\('node-fetch'\)\`. Upon starting the server, Node.js throws \`Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module ... not supported\`. The developer checks the node-fetch changelog and realizes v3 is ESM-only. They initially try renaming the file to \`.mjs\`, but this breaks other CommonJS imports in the codebase. Finally, they refactor to use dynamic import: \`const fetch = \(...args\) => import\('node-fetch'\).then\(\(\{default: fetch\}\) => fetch\(...args\)\);\`, which allows the ESM module to be loaded asynchronously within the CJS context, resolving the interop error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:08:51.126008+00:00— report_created — created