{"record":{"id":"7f4bbf790d44f2c6","repo":"mastra-ai/mastra","slug":"snapshot-functionality-requires-a-node-js-environm","errorCode":null,"errorMessage":"Snapshot functionality requires a Node.js environment. import.meta.url is not available in this runtime.","messagePattern":"Snapshot functionality requires a Node\\.js environment\\. import\\.meta\\.url is not available in this runtime\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"observability/mastra/src/exporters/test.ts","lineNumber":23,"sourceCode":" * - In-memory event collection for ALL signals (Traces, Metrics, Logs, Scores, Feedback)\n * - File output support\n * - Span lifecycle tracking and validation\n * - Query methods for filtering spans by type, trace ID, span ID, etc.\n * - Query methods for filtering logs, metrics, scores, and feedback\n * - Statistics and analytics on all collected signals\n * - Internal metrics collection with summary on flush()\n */\n\n/**\n * Lazily compute the snapshots directory.\n * Node.js-only: uses dynamic imports so the module can be loaded in edge runtimes\n * without failing at import time.\n */\nlet _snapshotsDir: string | undefined;\nasync function getSnapshotsDir(): Promise<string> {\n  if (!_snapshotsDir) {\n    if (typeof import.meta.url !== 'string') {\n      throw new Error(\n        'Snapshot functionality requires a Node.js environment. ' + 'import.meta.url is not available in this runtime.',\n      );\n    }\n    const { fileURLToPath } = await import('node:url');\n    const { dirname, join } = await import('node:path');\n    const __filename = fileURLToPath(import.meta.url);\n    const __dirname = dirname(__filename);\n    _snapshotsDir = join(__dirname, '..', '__snapshots__');\n  }\n  return _snapshotsDir;\n}\n\nimport type {\n  TracingEvent,\n  TracingEventType,\n  AnyExportedSpan,\n  ExportedSpan,\n  SpanType,","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/observability/mastra/src/exporters/test.ts#L5-L41","documentation":"The TestExporter's snapshot feature resolves its snapshot directory from this module's own file path via import.meta.url. This is only valid in Node.js (or runtimes implementing import.meta.url and node: builtins). The library throws this lazily — at snapshot time, not import time — when the runtime doesn't provide import.meta.url, so bundles targeting browsers/edge runtimes fail with a clear message instead of an opaque TypeError.","triggerScenarios":"Calling snapshotPath (or any snapshot API on TestExporter, e.g. export with updateSnapshot) from code bundled/executed in a non-Node runtime such as a browser, edge worker, or sandbox where import.meta.url is undefined or not a string.","commonSituations":"Bundling test-exporter code with a browser/edge platform target; executing in Cloudflare Workers or other edge sandboxes; importing the module into SSR/browser observability tooling.","solutions":["Run the code in Node.js — the snapshot feature is Node-only by design","Change the bundler platform/target to 'node' so import.meta.url and node: builtins are preserved","Keep snapshot-dependent modules out of client/edge bundles; gate snapshot usage behind a Node-only code path","Use a non-snapshot exporter (e.g. console/in-memory exporter) in non-Node runtimes"],"exampleFix":"// before (edge/browser bundle)\nexporter.export(spans, () => {}); // snapshot path -> throws\n// after\nif (typeof process !== 'undefined' && process.versions?.node) {\n  exporter.export(spans, () => {});\n} else {\n  inMemoryExporter.export(spans, () => {});\n}","handlingStrategy":"fallback","validationCode":"if (typeof import.meta.url !== 'string' || typeof process === 'undefined' || !process.versions?.node) {\n  throw new Error('Snapshot APIs require Node.js; use a non-snapshot exporter in this runtime.');\n}","typeGuard":"function isNodeRuntime(): boolean {\n  return (\n    typeof import.meta.url === 'string' &&\n    typeof process !== 'undefined' &&\n    !!process.versions?.node\n  );\n}","tryCatchPattern":"try {\n  exporter.export(spans, callback);\n} catch (err) {\n  if ((err as Error).message.includes('import.meta.url is not available')) {\n    fallbackExporter.export(spans, callback); // non-snapshot path\n  } else {\n    throw err;\n  }\n}","preventionTips":["Keep snapshot-dependent modules out of browser/edge bundles (platform: 'node' in bundler config)","Gate snapshot usage behind a runtime check before constructing TestExporter","Use a plain in-memory exporter for non-Node targets"],"tags":["nodejs","runtime","snapshots","environment"],"backgroundTag":"node-only-module-in-browser","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}