{"record":{"id":"b9b204827685db5a","repo":"mastra-ai/mastra","slug":"no-env-file-found-for-deploy-add-a-env-or-env","errorCode":null,"errorMessage":"No env file found for deploy. Add a .env or .env.* file before deploying.","messagePattern":"No env file found for deploy\\. Add a \\.env or \\.env\\.\\* file before deploying\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/deploy.ts","lineNumber":131,"sourceCode":"  projectDir: string,\n  options: { autoAccept?: boolean; envFile?: string } = {},\n): Promise<Record<string, string>> {\n  // When an explicit env file is provided, trust the user — read it directly.\n  if (options.envFile) {\n    const filePath = join(projectDir, options.envFile);\n    try {\n      await access(filePath);\n    } catch {\n      throw new Error(`Env file not found: ${options.envFile}`);\n    }\n    p.log.step(`Using env file: ${options.envFile}`);\n    return parseEnvFile(await readFile(filePath, 'utf-8'));\n  }\n\n  const availableDeployEnvFiles = await getDeployEnvFiles(projectDir);\n\n  if (availableDeployEnvFiles.length === 0) {\n    throw new Error('No env file found for deploy. Add a .env or .env.* file before deploying.');\n  }\n\n  let selectedEnvFile: string;\n\n  if (availableDeployEnvFiles.length === 1) {\n    selectedEnvFile = availableDeployEnvFiles[0]!;\n  } else if (options.autoAccept) {\n    throw new Error(\n      `Multiple env files found: ${availableDeployEnvFiles.join(', ')}. Use --env-file to specify which one to deploy.`,\n    );\n  } else {\n    const defaultFile =\n      availableDeployEnvFiles.find(envFile => envFile === '.env.production') ?? availableDeployEnvFiles[0]!;\n\n    const selected = await p.select({\n      message: 'Choose env file to deploy',\n      options: availableDeployEnvFiles.map(envFile => ({ value: envFile, label: envFile })),\n      initialValue: defaultFile,","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/deploy.ts#L113-L149","documentation":"When no --env-file is given, `readEnvVars` scans the project directory for deployable env files (.env, .env.*). If none exist, it throws this error because a server deploy requires environment variables to be packaged.","triggerScenarios":"Running `mastra server deploy` without --env-file in a project directory containing no .env or .env.* files.","commonSituations":"Fresh checkout where .env is gitignored and never created; CI environment without secrets materialized; renamed env files (e.g. .environment) the scanner doesn't recognize.","solutions":["Create a .env file in the project root with the required variables","Or pass an explicit file: `mastra server deploy --env-file <path>`","Restore/copy the env file in CI from your secrets manager"],"exampleFix":"// before\n# no .env in project\n// after\nprintf 'MASTRA_API_KEY=xxx\\n' > .env && mastra server deploy","handlingStrategy":"validation","validationCode":"import { readdirSync } from 'node:fs';\nconst envFiles = readdirSync(projectDir).filter(f => f === '.env' || f.startsWith('.env.'));\nif (envFiles.length === 0) {\n  throw new Error('Create a .env file before deploying.');\n}","typeGuard":"const hasEnvFile = (dir: string): boolean =>\n  readdirSync(dir).some(f => f === '.env' || f.startsWith('.env.'));","tryCatchPattern":"try {\n  await runServerDeploy(opts);\n} catch (e) {\n  if ((e as Error).message.startsWith('No env file found')) {\n    console.error('Add a .env or .env.* file to the project root.');\n  } else throw e;\n}","preventionTips":["Commit a .env.example and generate .env in setup steps","Inject env files from your secrets manager in CI","Document required env vars for deploys","Run a pre-deploy check for env file presence"],"tags":["env","deploy","cli","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}