Report #51438
[gotcha] ESM import.meta.url is a file:// URL while CJS \_\_dirname is a system path, causing malformed paths when mixing path.join\(\) with import.meta.url on Windows
Always use url.fileURLToPath\(\) to convert import.meta.url to a system path for fs operations; use url.pathToFileURL\(\) to convert \_\_dirname to a file URL for dynamic import\(\); never mix path.win32 methods with URL.pathname
Journey Context:
ES modules standardize on URLs \(file:// protocol\) for module resolution, while CommonJS uses filesystem strings. On Windows, this creates a triple trap: import.meta.url contains forward slashes and percent-encoding \(spaces become %20\), while \_\_dirname contains backslashes. Using path.join\(\_\_dirname, import.meta.url\) creates invalid strings. Using new URL\('./file', import.meta.url\) is correct for ESM, but trying to pass that to fs.readFileSync requires fileURLToPath. The alternative of using createRequire\(import.meta.url\) to get \_\_dirname in ESM works but is a workaround; the robust pattern is the URL/path conversion utilities.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:49:54.289813+00:00— report_created — created