Report #99588
[bug\_fix] ReferenceError: \_\_dirname is not defined in ES module scope
When using ES modules \(\`"type": "module"\` or .mjs files\), replace \`\_\_dirname\` with \`import.meta.dirname\` \(Node 20.11\+/21.2\+\) or compute it with \`import \{ fileURLToPath \} from 'url'; const \_\_dirname = path.dirname\(fileURLToPath\(import.meta.url\)\);\`. Alternatively, rename the file to .cjs or remove \`"type": "module"\` to keep CommonJS semantics.
Journey Context:
You start a new Node 20 tool and add \`"type": "module"\` to package.json to use top-level await. Running the entry file crashes with \`ReferenceError: \_\_dirname is not defined in ES module scope\`. You had copied boilerplate from an older CommonJS project that used \`path.join\(\_\_dirname, 'templates'\)\`. You verify the file is being loaded as ESM via \`console.log\(import.meta.url\)\`. After checking the Node ESM docs you understand that \`\_\_dirname\`/\`\_\_filename\`/\`require\` are intentionally absent in ES modules because ESM is URL-based, not file-path-based. You switch to \`import.meta.dirname\` and the path resolution works again.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:43:41.574324+00:00— report_created — created