mastra-ai/mastra · error · Error
Project ${project.name} has multiple environments (${slugs})
Error message
Project ${project.name} has multiple environments (${slugs}). Pass an environment name (e.g. `mastra env db create <env> --kind ...`) or use --shared to attach a project-scoped database. What it means
When a project has multiple environments, resolveDefaultEnvironment() cannot pick a default safely in non-interactive mode (or with --json), so it throws listing all environment slugs and asking the caller to pass an environment name or use --shared. Interactive sessions instead prompt with production pre-selected.
Source
Thrown at packages/cli/src/commands/db/db.ts:364
export async function resolveDefaultEnvironment(
token: string,
orgId: string,
project: Project,
opts: { json?: boolean } = {},
): Promise<Environment> {
const environments = await fetchEnvironments(token, orgId, project.id);
if (environments.length === 0) {
throw new Error(`Project ${project.name} has no environments. Create one with: mastra env create <name>`);
}
if (environments.length === 1) {
return environments[0]!;
}
if (opts.json || !isInteractive()) {
const slugs = environments.map(e => e.slug).join(', ');
throw new Error(
`Project ${project.name} has multiple environments (${slugs}). Pass an environment name (e.g. ` +
`\`mastra env db create <env> --kind ...\`) or use --shared to attach a project-scoped database.`,
);
}
// Prefer production as the pre-selected option when it exists.
const preferred = environments.find(e => e.type === 'production') ?? environments[0]!;
const selected = await p.select({
message: 'Which environment should this database be scoped to?',
initialValue: preferred.id,
options: environments.map(e => ({
value: e.id,
label: `${e.slug} (${e.type})`,
hint: e.region ?? undefined,
})),
});
View on GitHub (pinned to 75dd419e61)
Solutions
- Pass the target environment explicitly: `mastra env db create <env> --kind ...`.
- Use `--shared` to create a project-scoped database when no environment binding is wanted.
- Run the command interactively once to see a picker, then encode the chosen environment in your script.
Example fix
// before mastra env db create --kind turso # CI, multiple environments // after mastra env db create production --kind turso
Defensive patterns
Strategy: validation
Validate before calling
const envs = await listEnvironments();
if (envs.length > 1 && !explicitEnv && !shared) {
throw new Error(`Multiple environments (${envs.map(e => e.slug).join(', ')}): pass one explicitly or use --shared.`);
} Try / catch
try {
await dbCreate();
} catch (e) {
if (/has multiple environments/.test((e as Error).message)) {
const slugs = (e as Error).message.match(/\(([^)]+)\)/)?.[1] ?? '';
console.error(`Re-run with an explicit env, e.g. the first of: ${slugs}`);
}
} Prevention
- Always pass an explicit environment in CI/non-interactive scripts.
- Use --shared when the database should be project-scoped regardless of environments.
- Parse the slug list from the error message to select a valid environment programmatically.
When it happens
Trigger: Running `mastra env db create --kind ...` (no env arg, no --shared) with --json set or in CI/non-TTY where isInteractive() is false, against a project with 2+ environments.
Common situations: CI pipelines and scripted deployments (non-interactive by nature), or --json output mode where prompting is impossible.
Related errors
- Not logged in. Run `mastra auth login` interactively or set
- Session expired. Run `mastra auth login` interactively or se
- Environment not found: ${envArg}. List environments with: ma
- Project ${project.name} has no environments. Create one with
- Found ${projects.length} existing project(s) in this organiz
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/ba97539f48c3e0d9.
Report an issue: GitHub.