mastra-ai/mastra · error

Could not determine project name from package.json. Use --pr

Error message

Could not determine project name from package.json. Use --project to specify one.

What it means

`mastra server deploy` needs a project name when it falls back to creating/auto-selecting a project. resolveProject derives that default name from package.json (e.g. the package name); when no --project flag, MASTRA_PROJECT_ID env var, or linked .mastra-project.json project is available AND package.json yields no usable name, it throws so it doesn't create a nameless project.

Source

Thrown at packages/cli/src/commands/server/deploy.ts:288

      }
    }

    const created = await createServerProject(token, orgId, flagProject);
    p.log.success(`Created project "${created.name}"`);
    return { projectId: created.id, projectName: created.name, projectSlug: created.slug ?? created.name };
  }

  if (projectConfig?.projectId && projectConfig.organizationId === orgId) {
    return {
      projectId: projectConfig.projectId,
      projectName: projectConfig.projectName ?? projectConfig.projectId,
      projectSlug: projectConfig.projectSlug ?? projectConfig.projectName ?? projectConfig.projectId,
    };
  }

  const name = defaultName;
  if (!name) {
    throw new Error('Could not determine project name from package.json. Use --project to specify one.');
  }

  const existing = await fetchServerProjects(token, orgId);
  const nameMatches = existing.filter(proj => proj.name === name || proj.slug === name);

  if (existing.length > 0) {
    if (autoAccept) {
      // Non-interactive: only safe to auto-pick when exactly one project matches by name/slug.
      if (nameMatches.length === 1) {
        const m = nameMatches[0]!;
        return { projectId: m.id, projectName: m.name, projectSlug: m.slug ?? m.name };
      }
      throw new Error(
        `Found ${existing.length} existing project(s) in this organization. Pass --project <id-or-slug> to select one, or re-run without --yes to choose interactively.`,
      );
    }

    const CREATE_NEW = '__create_new__';

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass an explicit project: `mastra server deploy --project <id-or-slug-or-new-name>`
  2. Set MASTRA_PROJECT_ID in the environment before deploying
  3. Add a valid non-empty "name" field to package.json in the deploy directory
  4. Deploy from the directory containing a .mastra-project.json linked to the correct organization (matching organizationId)

Example fix

// before (package.json)
{ "private": true }
// after
{ "name": "my-mastra-app", "private": true }
Defensive patterns

Strategy: validation

Validate before calling

import { readFile } from 'node:fs/promises';
export async function assertProjectNameResolvable(dir: string, opts: { project?: string } = {}) {
  if (process.env.MASTRA_PROJECT_ID || opts.project) return;
  try {
    const pkg = JSON.parse(await readFile(`${dir}/package.json`, 'utf-8'));
    if (typeof pkg.name === 'string' && pkg.name.trim()) return;
  } catch {}
  throw new Error(`No project name resolvable in ${dir}. Pass --project or fix package.json "name".`);
}

Type guard

function hasProjectName(pkg: unknown): pkg is { name: string } {
  return typeof pkg === 'object' && pkg !== null &&
    typeof (pkg as any).name === 'string' && (pkg as any).name.trim().length > 0;
}

Try / catch

try {
  await run(['mastra', 'server', 'deploy', '--project', projectName]);
} catch (err) {
  if (err instanceof Error && err.message.includes('Could not determine project name')) {
    console.error('Set package.json "name" or pass --project <id-or-slug>');
  }
  throw err;
}

Prevention

When it happens

Trigger: Running `mastra server deploy` (with or without --yes) in a directory where: package.json is missing or unreadable, its `name` field is empty/invalid, and none of MASTRA_PROJECT_ID, --project, or a matching .mastra-project.json (projectId with matching organizationId) is set.

Common situations: Deploying from a workspace root or scratch directory without package.json; a monorepo sub-package with an empty "name"; CI job that checks out only a subfolder; user assumes .mastra-project.json exists but it was gitignored/deleted or was written for a different org (organizationId mismatch makes it ignored).

Related errors


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