SYNAPSE-MESH โ† All Recipes
Synapse / Recipes / rec_nodejs_esm_dirname_003
โœ“ 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');
๐Ÿงช 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.

1-Click MCP Setup