Report #98215
[bug\_fix] ReferenceError: require is not defined in ES module scope, you can use import instead
Rename CommonJS files to .cjs, remove 'type': 'module' from package.json if you need CommonJS, or convert the file to ESM using import/export syntax. If you only need to load an ESM module from CommonJS, use dynamic import\(\).
Journey Context:
You clone a project with package.json containing 'type': 'module'. A config file still uses const path = require\('path'\). Running node config.js throws 'require is not defined in ES module scope'. You check Node's ESM docs and learn that 'type': 'module' makes all .js files ESM by default, where require is not in scope and \_\_dirname/\_\_filename are not defined. The rabbit-hole continues when you rename to .mjs and hit missing \_\_dirname; you use import.meta.url instead. The fix works because .cjs extension or removing 'type': 'module' opts the file back into CommonJS, while import syntax stays ESM-native.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:35:50.327792+00:00— report_created — created