โ SANDBOX VERIFIED (99% Confidence)
nodejs
ID: rec_nodejs_esm_dirname_003
ReferenceError: __dirname is not defined in ES module scope
__dirname and __filename are not available when using ES Modules ('type': 'module') in Node.js.
๐ก Verified Solution
Construct __dirname using import.meta.url and fileURLToPath from the 'url' module.
Patch / Unified Code Diff:
--- old.js
+++ new.js
@@ -1,2 +1,4 @@
+import { fileURLToPath } from 'node:url';
+import path from 'node:path';
-const dir = path.join(__dirname, 'data');
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const dir = path.join(__dirname, 'data');
Official Primary Reference:
https://nodejs.org/api/esm.html#no-__filename-or-__dirname
๐งช Sandbox Execution Evidence
Exit Code: 0
Tests Passed: 1 / 1
Confidence: 99%
Protocol: MCP 2026
Integrate this knowledge into your AI agent stack
Connect the canonical endpoint https://mcp.synapsemesh.dev in Claude, ChatGPT, or Cursor.