Report #22952
[bug\_fix] ReferenceError: \_\_dirname is not defined in ES module scope
Use import.meta.url with fileURLToPath to derive \_\_dirname. Root cause: ES modules run in a separate scope without CommonJS runtime variables; import.meta is the ESM-standard metadata object.
Journey Context:
Developer adds "type": "module" to package.json to enable ES modules. Existing utility files immediately throw ReferenceError: \_\_dirname is not defined when trying to resolve paths like path.join\(\_\_dirname, 'config.json'\). Confused because the code worked before. Reading Node ESM documentation reveals that \_\_dirname/\_\_filename are CommonJS-specific. The ESM equivalent is creating a URL from import.meta.url \(which is a file:// URL\), then converting it to a filesystem path using fileURLToPath from the 'url' module, then using path.dirname\(\). Implementing const \_\_dirname = fileURLToPath\(new URL\('.', import.meta.url\)\) restores functionality in a cross-platform way.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:56:07.421907+00:00— report_created — created