{"record":{"id":"9f6185954c1e0695","repo":"mastra-ai/mastra","slug":"mastra-output-index-mjs-not-found-did-the-build-9f6185","errorCode":null,"errorMessage":".mastra/output/index.mjs not found — did the build succeed?","messagePattern":"\\.mastra/output/index\\.mjs not found — did the build succeed\\?","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/studio/deploy.ts","lineNumber":556,"sourceCode":"  } else if (staleness.isStale) {\n    // Build is stale or doesn't exist — rebuild\n    t = performance.now();\n    if (staleness.reason === 'hash-mismatch') {\n      p.log.step('Source files changed, rebuilding...');\n    }\n    await runBuild(targetDir, { debug: opts.debug });\n    p.log.step(`Build completed (${elapsed(performance.now() - t)})`);\n  } else {\n    // Build is up-to-date — skip rebuild\n    p.log.step('Build is up-to-date, skipping rebuild');\n  }\n\n  // Verify build output exists\n  const outputEntry = join(targetDir, '.mastra', 'output', 'index.mjs');\n  try {\n    await access(outputEntry);\n  } catch {\n    throw new Error('.mastra/output/index.mjs not found — did the build succeed?');\n  }\n\n  // If the user didn't pass --env-file and no ambient .env* file exists,\n  // skip the local env-var upload entirely and let the platform use the\n  // env vars stored on the project. The server-side deploy handler merges\n  // request envVars over the stored vars, so an empty (absent) envVars\n  // payload cleanly falls back to what's already stored.\n  let envVars: Record<string, string> = {};\n  const hasEnvFile = opts.envFile ? true : (await getDeployEnvFiles(targetDir)).length > 0;\n  if (hasEnvFile) {\n    envVars = await readEnvVars(targetDir, { autoAccept, envFile: opts.envFile });\n  }\n  const envCount = Object.keys(envVars).length;\n  if (envCount > 0) {\n    p.log.step(`Found ${envCount} env var(s)`);\n  } else if (hasEnvFile) {\n    p.log.step('No env vars found in selected env file');\n  } else {","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/studio/deploy.ts#L538-L574","documentation":"After building, deploy verifies the bundled entrypoint `.mastra/output/index.mjs` exists via fs access. If it does not, the build silently failed or produced output elsewhere, and the command throws before attempting upload — deploying a missing artifact would always fail server-side.","triggerScenarios":"`mastra deploy`/`mastra build` step failed or was skipped; building in a different directory than targetDir; custom build output configuration redirecting output away from .mastra/output; a partially cleaned .mastra directory.","commonSituations":"CI caches restoring a stale/empty .mastra folder; TypeScript/build errors swallowed by `|| true` in scripts; running deploy before ever running build; monorepo cwd mismatch so deploy checks the wrong directory.","solutions":["Run `mastra build` first and fix any build errors it reports","Ensure deploy runs in the same directory (targetDir) where the build wrote .mastra/output/index.mjs","Check for build script failures in CI logs (remove error-swallowing like `|| true`)","Clear .mastra and rebuild if output was partially cleaned or cached","If using custom output config, point it back to .mastra/output or pass the correct dir to deploy"],"exampleFix":"// before (CI)\n- run: mastra studio deploy --yes\n// after\n- run: mastra build\n- run: ls .mastra/output/index.mjs\n- run: mastra studio deploy --yes","handlingStrategy":"validation","validationCode":"import { access } from 'node:fs/promises';\nconst entry = join(deployDir, '.mastra', 'output', 'index.mjs');\ntry { await access(entry); } catch {\n  throw new Error('Run `mastra build` before deploy; .mastra/output/index.mjs is missing.');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await studioDeploy();\n} catch (e) {\n  if (e instanceof Error && e.message.includes('index.mjs not found')) {\n    console.error('Build first: `mastra build`. Inspect build logs for silent failures.');\n    process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Always run `mastra build` immediately before deploy in the same directory","Fail CI on any build-script error (no `|| true`)","Verify .mastra/output/index.mjs exists as a CI gate before deploy","Invalidate stale .mastra caches between builds","Keep custom build output config pointed at .mastra/output"],"tags":["cli","build","filesystem"],"backgroundTag":"build-artifact-missing","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}