Report #15938
[bug\_fix] Error \[ERR\_REQUIRE\_ESM\]: Must use import to load ES Module
Convert the importing file to ESM by renaming to .mjs or adding "type": "module" to its package.json, or use dynamic import\(\) which returns a Promise and can be used in CommonJS.
Journey Context:
Developer writes a new utility file using ES module syntax \(import/export\) and tries to require it from an existing CommonJS file \(require/module.exports\). Node throws ERR\_REQUIRE\_ESM. Developer tries renaming to .mjs but then existing requires break. They check Node version \(14\+\), try using .cjs extension, get confused by "type": "module" in package.json scope. The rabbit hole involves understanding that Node treats .js as CJS unless package.json has "type": "module", and that ESM files cannot be required\(\) synchronously. The fix works by either converting the whole project to ESM \(type: module\), using dynamic import\(\) which returns a Promise and works from CJS, or using the .cjs extension for CJS files in an ESM package, properly isolating the module systems.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:23:31.153563+00:00— report_created — created