{"record":{"id":"36af2178040750d5","repo":"mastra-ai/mastra","slug":"mastra-output-index-mjs-not-found-did-the-build","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/deploy/index.ts","lineNumber":748,"sourceCode":"    }\n    p.log.step('Skipping build (--skip-build)');\n  } else if (staleness.isStale) {\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    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  // Auto-select .env.<envName> when deploying to a named environment\n  // (e.g. --env staging auto-selects .env.staging if it exists).\n  //\n  // envName comes from the --env CLI flag, so we validate it before\n  // interpolating it into a file path. Only simple environment identifiers\n  // (letters, digits, dot, dash, underscore) are allowed; anything with a\n  // path separator or `..` traversal segment is ignored. This keeps a\n  // hostile --env value from escaping the project directory and being read\n  // (and re-uploaded) via readEnvVars.\n  let envFile = opts.envFile;\n  if (!envFile && /^[a-zA-Z0-9._-]+$/.test(envName) && !envName.includes('..')) {\n    const envNameFile = `.env.${envName}`;\n    const candidate = resolve(targetDir, envNameFile);\n    const targetPrefix = resolve(targetDir) + '/';\n    if (candidate.startsWith(targetPrefix)) {\n      try {","sourceCodeStart":730,"sourceCodeEnd":766,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/deploy/index.ts#L730-L766","documentation":"Thrown by runUnifiedDeploy after the build step completes, when it verifies that the build entry point .mastra/output/index.mjs exists via fs.access. If the file is missing, the build either did not run, failed silently, or wrote output elsewhere — so the CLI refuses to package and upload an incomplete deploy. The message explicitly suggests checking whether the build succeeded.","triggerScenarios":"fs.access(join(targetDir, '.mastra', 'output', 'index.mjs')) rejects after the build phase. Occurs when the build command failed but the deploy continued, the build produced a different entry (e.g. index.js, or a non-bundled output), or a stale/cleaned .mastra directory.","commonSituations":"Build step failed earlier but errors were swallowed or scrolled past; running `mastra deploy` without `mastra build` in setups that expect a separate build; custom output configuration redirecting the bundle away from .mastra/output; running deploy from the wrong directory; stale .mastra/output removed by a git clean or CI cache reset; Mastra version changes altering the output path.","solutions":["Run `mastra build` first (or re-run `mastra deploy` watching the build output for errors) and fix any build failures.","Confirm you are deploying from the project root that contains the .mastra directory (pass the correct dir argument).","Inspect .mastra/output to see what the build actually produced; check for custom output config redirecting the entry file.","Clear .mastra/output and rebuild to eliminate stale/corrupted artifacts; verify your @mastra/core and CLI versions are in sync."],"exampleFix":"// before: deploying without a valid build output\n$ mastra deploy\nError: .mastra/output/index.mjs not found — did the build succeed?\n// after: build first, then deploy\n$ mastra build\n$ ls .mastra/output/index.mjs\n$ mastra deploy","handlingStrategy":"validation","validationCode":"import { access } from 'node:fs/promises';\ntry {\n  await access('.mastra/output/index.mjs');\n} catch {\n  throw new Error('Run `mastra build` before `mastra deploy`');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run `mastra build` (or a full `mastra deploy` that includes build) before deploying.","Check the build step's exit status in CI; fail the pipeline on build errors.","Do not clean or gitignore-strip .mastra/output between build and deploy.","Keep CLI and @mastra/core versions aligned so the output path is what the deployer expects."],"tags":["build","deploy","filesystem","cli"],"backgroundTag":"build-artifact-missing","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}