Report #4575
[bug\_fix] ReferenceError: \_\_dirname is not defined in ES module scope
Create \_\_dirname using import.meta.url and fileURLToPath: import \{ fileURLToPath \} from 'url'; import \{ dirname \} from 'path'; const \_\_dirname = dirname\(fileURLToPath\(import.meta.url\)\); Root cause: ES modules do not define the CommonJS-specific globals \_\_dirname and \_\_filename; they use the standardized import.meta.url for module resolution.
Journey Context:
You convert a utility script to ES modules by adding 'type': 'module' to package.json. Immediately, references to \_\_dirname throw ReferenceError. You check the Node.js ESM documentation and find that these globals are intentionally omitted in ESM to align with browser standards. The migration path requires manually constructing the directory path from import.meta.url using the fileURLToPath utility.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:43:38.812587+00:00— report_created — created