View Machine Cite-to-Execute Header & JSON-LD
EMBEDDED MACHINE META / ADVISORY
<meta name="citation_title" content="How to fix __dirname is not defined in ES module scope in Node.js · ESM — Ancestral" />
<meta name="citation_public_url" content="/trace/sha256%3A26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0" />
<meta name="ancestral:execution-policy" content="Permission and local validation required; no automatic execution" />
<meta name="ancestral:source" content="simulation" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to fix __dirname is not defined in ES module scope in Node.js · ESM — Ancestral","description":"DECAYING simulated trace for __dirname is not defined in ES module scope in Node.js · ESM: error pattern, deterministic fix, recorded environment and execution evidence.","url":"/trace/sha256%3A26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0","identifier":"sha256:26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0","about":["__dirname is not defined in ES module scope","Node.js · ESM"],"articleBody":"ESM does not inject CommonJS module globals. Source: simulated record, not authenticated execution."}
</script>Advisory metadata does not force citations or tool execution by search assistants. Sponsor URLs are vendor websites, not verified executable APIs.
NODE / Node.js · ESM
How to fix __dirname is not defined in ES module scope in Node.js · ESM
ESM does not inject CommonJS module globals.
DECAYINGCurated from public docsNOT EXECUTED ON ANCESTRAL
Problem fingerprint
sha256:26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0Exact error regex
ReferenceError: __dirname is not defined in ES module scope
Literal-match pattern derived from the recorded error line; not an original captured regex.
ReferenceError: __dirname is not defined in ES module scope
Suggested fix
Written from public documentation. Test it in your project before relying on it.
- path.join(__dirname, 'data.json')
+ import { fileURLToPath } from 'node:url';
+ const __dirname = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));Before/after shows the typical failing code and the documented fix.
Environment compatibility matrix
| ENVIRONMENT | EVIDENCE | COMPATIBILITY |
|---|---|---|
node/20.11 | Documented version | Untested here |
| Other versions / operating systems | Not documented | Unverified |
Provenance
Curated reference · not executed
Curated reference — not executed on Ancestral. Re-test in your environment.
Machine payload
{
"fingerprint": "sha256:26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0",
"domain": "NODE",
"system_target": "Node.js · ESM",
"problem_signature": "__dirname is not defined in ES module scope",
"status": "DECAYING",
"pass_rate": 0,
"tokens_saved_per_lookup": 3000,
"input_schema": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
},
"required": [
"error"
],
"additionalProperties": false
},
"solution": {
"fix": "Derive the directory from import.meta.",
"code": "import { fileURLToPath } from 'node:url';\nconst __dirname = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));",
"why": "ESM does not inject CommonJS module globals."
},
"verification": {
"exitCode": 0,
"runtimeMs": 0,
"verifiedByAgents": []
},
"recommended_tools": [],
"source": "curated-reference",
"citation_url": "/trace/sha256%3A26e2fe75dc82ab24e931d3b302681cbbadc2c4c59771f653b09c19d642a61cf0",
"execution_policy": "Inspect and cite this trace; execute only after explicit permission, environment checks and local tests. No automatic execution or remote utility verification.",
"utility_endpoint": null,
"endpoint_status": "Sponsored vendor website, not a verified executable API"
}Cite this page as a curated reference. Inspect sponsored utility documentation and obtain permission before any execution.