Report #97698
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module not supported
Convert the importing file to ES module syntax \(e.g., use \`import\` instead of \`require\(\)\`\), or change the file extension to \`.cjs\` to use CommonJS, or set \`"type": "module"\` in package.json and use \`import\`.
Journey Context:
I was working on a Node.js project that used \`require\(\)\` to load a library that had recently switched to ESM-only distribution \(like \`node-fetch\` v3\). Node.js threw ERR\_REQUIRE\_ESM because CommonJS \`require\(\)\` cannot load ES modules. I tried adding \`"type": "module"\` to package.json, but then all my \`.js\` files became ESM, breaking other \`require\(\)\` calls. The solution was to rename my file from \`index.js\` to \`index.cjs\` \(forcing CommonJS\), or rewrite the importing file as ESM with \`import\` statements. I chose to use dynamic \`import\(\)\` inside a CommonJS file: \`const fetch = \(await import\('node-fetch'\)\).default;\`. This is the documented workaround for mixing module systems.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T15:52:49.184757+00:00— report_created — created