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
- Verify the file exists at the printed absolute path (ls -la) and fix the path/typo
- Run the command from the directory containing the env file or pass an absolute path
- Check file permissions (chmod u+r) and that it is a regular file, not a directory
- 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
- Pass absolute paths in scripts and CI
- Verify the env file exists (and isn't gitignored away) before the import step
- Set the correct working directory before running the command
- Quote paths in shell commands to avoid truncation
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
- Directory ${path.basename(targetPath)} already exists
- Project name must be 1-214 lowercase characters, start with
- A file or directory named "${projectName}" already exists. P
- .mastra/output/index.mjs not found — did the build succeed?
- Directory not found: ${dirArg}.${hint}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/c53c1d0683f275e2.
Report an issue: GitHub.