angular/components · error · SchematicsException
Project name is required.
Error message
Project name is required.
What it means
getProjectFromWorkspace receives projectName as string | undefined because some schematic APIs allow optional project names; when it is undefined/empty the function throws this SchematicsException instead of silently guessing the default project.
Source
Thrown at src/cdk/schematics/utils/get-project.ts:23
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {SchematicsException} from '@angular-devkit/schematics';
import {ProjectDefinition, WorkspaceDefinition} from '@schematics/angular/utility';
/**
* Finds the specified project configuration in the workspace. Throws an error if the project
* couldn't be found.
*/
export function getProjectFromWorkspace(
workspace: WorkspaceDefinition,
projectName: string | undefined,
): ProjectDefinition {
if (!projectName) {
// TODO(crisbeto): some schematics APIs have the project name as optional so for now it's
// simpler to allow undefined and checking it at runtime. Eventually we should clean this up.
throw new SchematicsException('Project name is required.');
}
const project = workspace.projects.get(projectName);
if (!project) {
throw new SchematicsException(`Could not find project in workspace: ${projectName}`);
}
return project;
}
View on GitHub (pinned to 0411926e7d)
Solutions
- Pass --project=<name> explicitly on the schematic command line.
- In your schematic, default the option before calling: projectName ?? workspace.extensions.defaultProject (or first entry of workspace.projects).
- Add a 'project' property with `$default: { "$source": "projectName" }` to the schematic's schema.json so the CLI injects it.
- Ensure the workspace config defines a defaultProject so the CLI's project-name source resolves.
Example fix
// before const project = getProjectFromWorkspace(workspace, options.project); // options.project: string | undefined // after const projectName = options.project ?? (workspace.extensions['defaultProject'] as string | undefined); const project = getProjectFromWorkspace(workspace, projectName);
Defensive patterns
Strategy: validation
Validate before calling
function resolveProjectName(
projectName: string | undefined,
workspace: WorkspaceDefinition,
): string {
const fallback = workspace.extensions['defaultProject'] as string | undefined;
const name = projectName ?? fallback;
if (!name) {
throw new Error('No --project given and no defaultProject in angular.json');
}
return name;
} Type guard
function hasProjectName(p: string | undefined): p is string {
return typeof p === 'string' && p.trim().length > 0;
} Try / catch
try {
const project = getProjectFromWorkspace(workspace, options.project);
} catch (e) {
if (e instanceof SchematicsException && e.message === 'Project name is required.') {
console.error('Pass --project=<name> to the schematic.');
return;
}
throw e;
} Prevention
- Always pass --project in multi-project workspaces.
- Declare the project option in schema.json with $default projectName source.
- Default options.project to the workspace defaultProject in your schematic's rule factory.
- Reject empty-string project names early at the CLI/option-parsing layer.
When it happens
Trigger: Calling getProjectFromWorkspace(workspace, options.project) where options.project is undefined — e.g. the schematic's schema doesn't declare a 'project' property with a default, the user omitted --project and no default was injected, or 'project' is an empty string.
Common situations: Custom schematics that call this util without registering the project option in schema.json, running `ng add`/`ng generate` in workspaces where the caller forgot --project, and newer Angular CLI versions (13+ WorkspaceDefinition API) where defaults are no longer auto-resolved by the util.
Related errors
- Cannot read "${filePath}" because it does not exist.
- No data could be found for target version: ${version}
- Could not find file for path: ${path}
- Module not found: ${modulePath}
- Could not read Angular module file: ${modulePath}
AI-assisted analysis of angular/components@0411926e7d (2026-08-31).
Data as JSON: /api/errors/737ac9b3fae24b55.
Report an issue: GitHub.