mastra-ai/mastra · error

Could not read file: ${filePath}

Error message

Could not read file: ${filePath}

What it means

`mastra server env import <file>` reads the given env file from disk before parsing and uploading it. Any fs read failure (missing file, permission denied, path is a directory, encoding issues) is wrapped into this generic 'Could not read file: <path>' error, so the underlying errno details are hidden.

Source

Thrown at packages/cli/src/commands/server/env.ts:119

  }

  delete envVars[key];
  await updateServerProjectEnv(token, orgId, projectId, envVars);

  console.info(`\n  Removed ${key} successfully.\n`);
}

/* ------------------------------------------------------------------ */
/*  mastra server env import                                           */
/* ------------------------------------------------------------------ */

export async function envImportAction(file: string, opts: { config?: string }) {
  const filePath = resolve(file);
  let content: string;
  try {
    content = await readFile(filePath, 'utf-8');
  } catch {
    throw new Error(`Could not read file: ${filePath}`);
  }

  const newVars = parseEnvFile(content);
  const newKeys = Object.keys(newVars);
  if (newKeys.length === 0) {
    console.info('\n  No variables found in file.\n');
    return;
  }

  const { token, orgId } = await resolveAuth();
  const projectId = await resolveProjectId(opts);

  // Merge with existing env vars (new values override existing)
  const envVars = await getServerProjectEnv(token, orgId, projectId);
  Object.assign(envVars, newVars);
  await updateServerProjectEnv(token, orgId, projectId, envVars);

  console.info(`\n  Imported ${newKeys.length} variable(s) from ${file}:\n`);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the file exists at the printed absolute path (ls -la) and fix the path/typo
  2. Run the command from the directory containing the env file or pass an absolute path
  3. Check file permissions (chmod u+r) and that it is a regular file, not a directory
  4. Confirm the file is committed/present in CI (not gitignored) before the import step

Example fix

// before
mastra server env import .env.prod   # run from wrong cwd, ENOENT
// after
mastra server env import "$(pwd)/.env.prod"
Defensive patterns

Strategy: validation

Validate before calling

import { stat } from 'node:fs/promises';
import { resolve } from 'node:path';
// Preflight before `mastra server env import <file>`
export async function assertReadableEnvFile(file: string) {
  const p = resolve(file);
  const s = await stat(p); // throws with real errno if missing
  if (!s.isFile()) throw new Error(`Not a file: ${p}`);
  return p;
}

Try / catch

try {
  await exec(`mastra server env import ${envFile}`);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Could not read file:')) {
    console.error(`Env file missing/unreadable: ${err.message}. Check cwd and path.`);
    process.exitCode = 1;
  } else throw err;
}

Prevention

When it happens

Trigger: Passing a relative path from the wrong cwd; filename typo (e.g. .env.local not committed); passing a directory instead of a file; file unreadable due to permissions; file deleted between tab-completion and execution.

Common situations: CI runs from a different working directory than the developer assumed; secrets files excluded by .gitignore so a fresh checkout lacks them; quoting issues dropping part of the path from the shell command.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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