Report #97144
[bug\_fix] ReferenceError: \_\_dirname is not defined in ES module scope
In an ESM file, replace \`\_\_dirname\` with \`const \_\_dirname = fileURLToPath\(new URL\('.', import.meta.url\)\)\` after importing \`fileURLToPath\` from \`node:url\` and \`dirname\` from \`node:path\` \(or use the equivalent \`path.dirname\` wrapper\). Alternatively, change \`"type": "module"\` back to CommonJS in package.json, or rename the file from \`.js\` to \`.cjs\` if you need \`require\`/\`\_\_dirname\` without changing imports.
Journey Context:
You add \`"type": "module"\` to package.json to use native ESM, then run the server and it immediately throws \`ReferenceError: \_\_dirname is not defined\`. You search and learn that \`\_\_dirname\`, \`\_\_filename\`, and \`require\` are CommonJS-only globals. ESM modules expose \`import.meta.url\` instead. You import \`fileURLToPath\` to convert the \`file://\` URL into a filesystem path, then derive the directory. If the file is loaded as a CommonJS \`.cjs\` file, the old globals work unchanged, which is useful for config files that must stay synchronous.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:37:29.999929+00:00— report_created — created