mastra-ai/mastra · error · Error
Directory ${projectName} already exists
Error message
Directory ${projectName} already exists What it means
cloneTemplate refuses to proceed when the destination directory (projectPath/projectName) already exists. It checks directoryExists(projectPath) before cloning and aborts with spinner.error plus this Error to avoid overwriting an existing project. This is a deliberate safety guard so `create-mastra` never clobbers user files.
Source
Thrown at packages/cli/src/utils/clone-template.ts:54
projectName: string;
targetDir?: string;
branch?: string;
signal?: AbortSignal;
silent?: boolean;
}
export async function cloneTemplate(options: CloneTemplateOptions): Promise<string> {
const { template, projectName, targetDir, branch, signal, silent = false } = options;
const projectPath = targetDir ? path.resolve(targetDir, projectName) : path.resolve(projectName);
const spinner = startSpinner(`Cloning template "${template.title}"...`, signal, silent);
let ownsProjectPath = false;
try {
// Check if directory already exists
if (await directoryExists(projectPath)) {
spinner?.error(`Directory ${projectName} already exists`);
throw new Error(`Directory ${projectName} already exists`);
}
ownsProjectPath = true;
// Clone the repository without git history
await cloneRepositoryWithoutGit(template.githubUrl, projectPath, branch, signal);
// Update package.json with new project name
await updatePackageJson(projectPath, projectName);
spinner?.success(`Template "${template.title}" cloned successfully to ${projectName}`);
return projectPath;
} catch (error) {
if (ownsProjectPath) {
await fs.rm(projectPath, { recursive: true, force: true });
}
spinner?.error(`Failed to clone template: ${error instanceof Error ? error.message : 'Unknown error'}`);
throw error;View on GitHub (pinned to 75dd419e61)
Solutions
- Choose a different project name or remove/rename the existing directory before re-running the CLI.
- If the existing directory is a leftover partial clone from a failed run, delete it (rm -rf <dir>) and retry.
- Run the CLI from a different working directory where the target name is free.
Example fix
// before $ npx create-mastra@latest my-app // Error: Directory my-app already exists // after $ rm -rf my-app # or pick a new name $ npx create-mastra@latest my-app-v2
Defensive patterns
Strategy: validation
Validate before calling
import { stat } from 'node:fs/promises';
async function directoryExists(p: string): Promise<boolean> {
try { return (await stat(p)).isDirectory(); } catch { return false; }
}
if (await directoryExists(targetPath)) {
throw new Error(`Directory ${projectName} already exists`);
} Prevention
- Pick a unique project name or check the target path before invoking create-mastra.
- Clean up partial directories left by aborted scaffold runs before retrying.
- Run the CLI with a fresh directory or pass a distinct --projectName in CI.
When it happens
Trigger: Running the create-mastra CLI (cloneTemplate) with a --projectName or target path whose directory already exists on disk (including empty ones, since only existence is checked, not emptiness).
Common situations: Re-running the scaffolder after a failed/aborted first attempt left a partial directory; choosing a name like 'my-app' that already exists in the cwd; rerunning the command in a repo where the target folder is committed; re-running in a workspace with a pre-created empty folder.
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/6aa0890331250b77.
Report an issue: GitHub.