mastra-ai/mastra · error · Error

Environment not found: ${envArg}

Error message

Environment not found: ${envArg}

What it means

The `mastra env vars pull` command resolves an environment by exact match on id, name, or slug against the environments fetched for the linked project. This error is thrown when the user-supplied environment argument matches none of the project's environments. It indicates a typo, a stale reference, or that the environment belongs to a different project/organization.

Source

Thrown at packages/cli/src/commands/env/vars.ts:44

function isAlreadyExistsError(error: unknown): boolean {
  return typeof error === 'object' && error !== null && 'code' in error && error.code === 'EEXIST';
}

function pickEnvironment(environments: Environment[], envArg: string | undefined): Environment {
  if (environments.length === 0) {
    throw new Error('No environments found for this project. Deploy first with `mastra deploy`.');
  }

  if (!envArg) {
    if (environments.length === 1) return environments[0]!;
    const slugs = environments.map(e => e.slug).join(', ');
    throw new Error(`Multiple environments found (${slugs}). Specify one: mastra env vars pull <environment>`);
  }

  const env = environments.find(e => e.id === envArg || e.name === envArg || e.slug === envArg);
  if (!env) {
    throw new Error(`Environment not found: ${envArg}`);
  }
  return env;
}

/**
 * Pull the full set of env vars that a deploy of the target environment
 * actually runs with — the environment row's vars (e.g. added via the UI
 * editor) merged with the project-scoped vars, with project values winning on
 * conflict, matching the platform's deploy-time merge precedence. Managed
 * vars (platform-injected secrets) are listed as comments, names only.
 *
 * The legacy `mastra server env pull` reads only the project scope; this is
 * the unified-surface replacement that fixes UI-added vars silently missing
 * from pulled files.
 */
export async function envVarsPullAction(
  envArg: string | undefined,
  options: { project?: string; output?: string; force?: boolean },

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Run `mastra env vars pull` with no argument: if the project has exactly one environment it is picked automatically; otherwise the error lists all valid slugs — copy one exactly.
  2. Check for typos/case and re-run with the exact slug or ID, e.g. `mastra env vars pull production`.
  3. Verify the environment belongs to the intended project by passing `--project <project>` explicitly.
  4. If the environment was renamed or deleted, update your scripts or recreate the environment and redeploy with `mastra deploy`.

Example fix

// before
mastra env vars pull Production
// after
mastra env vars pull production   # exact slug, from the list printed by the multiple-environments error
Defensive patterns

Strategy: validation

Validate before calling

const envs = await fetchEnvironments(token, orgId, project.id);
if (envArg && !envs.some(e => e.id === envArg || e.name === envArg || e.slug === envArg)) {
  throw new Error(`Unknown environment '${envArg}'. Valid: ${envs.map(e => e.slug).join(', ')}`);
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Running `mastra env vars pull <environment>` where <environment> does not exactly (case-sensitively) match the id, name, or slug of any environment returned by fetchEnvironments for the resolved project.

Common situations: Typo or wrong casing in the environment slug (matching is exact, not fuzzy); the environment was deleted or renamed; the environment belongs to a different project than the linked one; `--project` was omitted so the wrong linked project was resolved; being authenticated into a different org.

Related errors


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