angular/angular-cli · error · Error
Project name already exists.
Error message
Project name already exists.
What it means
WorkspaceDefinition.add() enforces unique project names. Before creating the ProjectDefinition it checks the underlying DefinitionCollection via has(name) and throws if a project with that name is already registered. Duplicate project names make angular.json targets/paths ambiguous, so the collection rejects them eagerly.
Source
Thrown at packages/angular_devkit/core/src/workspace/definitions.ts:150
export class ProjectDefinitionCollection extends DefinitionCollection<ProjectDefinition> {
constructor(
initial?: Record<string, ProjectDefinition>,
listener?: DefinitionCollectionListener<ProjectDefinition>,
) {
super(initial, listener);
}
add(definition: {
name: string;
root: string;
sourceRoot?: string;
prefix?: string;
targets?: Record<string, TargetDefinition | undefined>;
[key: string]: unknown;
}): ProjectDefinition {
if (this.has(definition.name)) {
throw new Error('Project name already exists.');
}
this._validateName(definition.name);
const project: ProjectDefinition = {
root: definition.root,
prefix: definition.prefix,
sourceRoot: definition.sourceRoot,
targets: new TargetDefinitionCollection(),
extensions: {},
};
if (definition.targets) {
for (const [name, target] of Object.entries(definition.targets)) {
if (target) {
project.targets.set(name, target);
}
}
}View on GitHub (pinned to bb72145f9a)
Solutions
- Check workspace.has(name) before adding and reuse/update the existing project instead.
- Generate a unique name (suffix the app name) when adding programmatically.
- Use set() or remove the existing project first if replacement is intended.
Example fix
// before
const project = workspace.addProject({ name: 'shared-ui', root: 'libs/shared-ui' });
// after
if (!workspace.has('shared-ui')) {
const project = workspace.addProject({ name: 'shared-ui', root: 'libs/shared-ui' });
} Defensive patterns
Strategy: validation
Validate before calling
if (workspace.has(name)) {
throw new Error(`Project '${name}' already exists in workspace`);
}
const project = workspace.addProject({ name, root }); Type guard
function canAddProject(ws: WorkspaceDefinition, name: string): boolean {
return typeof name === 'string' && !ws.has(name);
} Try / catch
try {
project = workspace.addProject({ name, root });
} catch (e) {
if (e.message === 'Project name already exists.') {
project = workspace.getProject(name); // reuse existing
} else throw e;
} Prevention
- Check workspace.has(name) before every addProject call
- Make schematics idempotent: skip creation if the project already exists
- Use set() when replacement semantics are intended
When it happens
Trigger: Calling workspace.addProject({ name: 'my-app', root: '...' }) (or similar add) when a project named 'my-app' already exists in the WorkspaceDefinition.
Common situations: Generator/schematic run twice on the same workspace; programmatic script adding projects in a loop without deduplication; case is significant, but identical names from re-running code generation collide.
Related errors
- Target name already exists.
- "${name}" must be a JSON value.
- Project name must be a valid npm package name.
- Target name must be a string.
- Unable to read workspace file.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/ed59e489f947d8f1.
Report an issue: GitHub.