Report #38673
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: require\(\) of ES Module not supported
Convert the importing file to an ES module by renaming it with the .mjs extension, or add 'type': 'module' to the project's package.json to enable ESM for all .js files. Alternatively, use dynamic import\(\) to load the ES module asynchronously: const \{ default: pkg \} = await import\('esm-package'\).
Journey Context:
Maintaining a legacy Express.js application using CommonJS \(require/module.exports\). Needed to upgrade the 'got' HTTP library from v11 to v12 for security fixes. After updating package.json and restarting the server, it crashed immediately with ERR\_REQUIRE\_ESM, stating that 'got' is now an ES module and cannot be required. Attempted to refactor the entire codebase to ESM by adding 'type': 'module', but this broke twenty other CommonJS dependencies that used require\(\). Finally, wrapped the got import in an async function using dynamic import\(\), which allowed the ES module to load while keeping the rest of the file as CommonJS.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:23:22.147578+00:00— report_created — created