mastra-ai/mastra · error

Multiple env files found: ${availableDeployEnvFiles.join(',

Error message

Multiple env files found: ${availableDeployEnvFiles.join(', ')}. Use --env-file to specify which one to deploy.

What it means

When multiple deployable env files exist and --auto-accept is set, the CLI cannot prompt to choose, so `readEnvVars` throws this error listing all candidates and instructing the use of --env-file. Interactive mode would prompt instead; auto-accept mode must be deterministic.

Source

Thrown at packages/cli/src/commands/server/deploy.ts:139

    } catch {
      throw new Error(`Env file not found: ${options.envFile}`);
    }
    p.log.step(`Using env file: ${options.envFile}`);
    return parseEnvFile(await readFile(filePath, 'utf-8'));
  }

  const availableDeployEnvFiles = await getDeployEnvFiles(projectDir);

  if (availableDeployEnvFiles.length === 0) {
    throw new Error('No env file found for deploy. Add a .env or .env.* file before deploying.');
  }

  let selectedEnvFile: string;

  if (availableDeployEnvFiles.length === 1) {
    selectedEnvFile = availableDeployEnvFiles[0]!;
  } else if (options.autoAccept) {
    throw new Error(
      `Multiple env files found: ${availableDeployEnvFiles.join(', ')}. Use --env-file to specify which one to deploy.`,
    );
  } else {
    const defaultFile =
      availableDeployEnvFiles.find(envFile => envFile === '.env.production') ?? availableDeployEnvFiles[0]!;

    const selected = await p.select({
      message: 'Choose env file to deploy',
      options: availableDeployEnvFiles.map(envFile => ({ value: envFile, label: envFile })),
      initialValue: defaultFile,
    });

    if (p.isCancel(selected)) {
      p.cancel('Deploy cancelled.');
      process.exit(0);
    }

    selectedEnvFile = selected as string;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Add `--env-file .env.production` (or the desired file) to the deploy command
  2. Remove or rename the extra env files so only one remains
  3. Drop --auto-accept and choose interactively

Example fix

// before
mastra server deploy --auto-accept   # found .env and .env.production
// after
mastra server deploy --auto-accept --env-file .env.production
Defensive patterns

Strategy: validation

Validate before calling

import { readdirSync } from 'node:fs';
const envFiles = readdirSync(projectDir).filter(f => f === '.env' || f.startsWith('.env.'));
if (isCi && envFiles.length > 1 && !opts.envFile) {
  throw new Error(`Pass --env-file; candidates: ${envFiles.join(', ')}`);
}

Type guard

const hasSingleEnvFile = (dir: string): boolean =>
  readdirSync(dir).filter(f => f === '.env' || f.startsWith('.env.')).length === 1;

Try / catch

try {
  await runServerDeploy(opts);
} catch (e) {
  if ((e as Error).message.startsWith('Multiple env files found')) {
    console.error('Add --env-file <name> to select one deterministically.');
  } else throw e;
}

Prevention

When it happens

Trigger: Running `mastra server deploy --auto-accept` (or a non-interactive pipeline) where getDeployEnvFiles returns 2+ files (.env, .env.production, .env.staging, etc.) and no --env-file is provided.

Common situations: CI/CD automation on repos containing several environment files; staging and production env files both present locally.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/49140fed985d26e3. Report an issue: GitHub.