angular/angular-cli · error · CommandModuleError
A workspace is required for this command.
Error message
A workspace is required for this command.
What it means
Many Angular CLI commands (build, serve, generate, etc.) only make sense inside an Angular workspace. getWorkspaceOrThrow is a guard that throws this CommandModuleError when the command's context has no loaded workspace.
Source
Thrown at packages/angular/cli/src/command-builder/command-module.ts:201
const optionsWithAnalytics = addSchemaOptionsToCommand(
localYargs,
options,
// This should only be done when `--help` is used otherwise default will override options set in angular.json.
/* includeDefaultValues= */ this.context.args.options.help,
);
// Record option of analytics.
for (const [name, userAnalytics] of optionsWithAnalytics) {
this.optionsWithAnalytics.set(name, userAnalytics);
}
return localYargs;
}
protected getWorkspaceOrThrow(): AngularWorkspace {
const { workspace } = this.context;
if (!workspace) {
throw new CommandModuleError('A workspace is required for this command.');
}
return workspace;
}
/**
* Flush on an interval (if the event loop is waiting).
*
* @returns a method that when called will terminate the periodic
* flush and call flush one last time.
*/
protected getAnalyticsParameters(
options: (Options<T> & OtherOptions) | OtherOptions,
): Partial<Record<EventCustomDimension | EventCustomMetric, string | boolean | number>> {
const parameters: Partial<
Record<EventCustomDimension | EventCustomMetric, string | boolean | number>
> = {};
View on GitHub (pinned to bb72145f9a)
Solutions
- cd into the directory containing angular.json before running the command
- Create a workspace with 'ng new' if you intended to start a project
- Use '--project' only after being inside a valid workspace; the cwd is what matters here
- For monorepos, configure tooling to run commands from the app subdirectory
Example fix
// before ~ $ ng g c header // after ~ $ cd dev/my-app && ng g c header
Defensive patterns
Strategy: validation
Validate before calling
import { existsSync } from 'fs';
if (!existsSync('angular.json') && !existsSync('.angular.json')) {
throw new Error('This command must run inside an Angular workspace (angular.json not found).');
} Try / catch
try {
execSync('ng generate component x', { stdio: 'inherit' });
} catch (e) {
if (String(e).includes('A workspace is required')) {
console.error('cd into your Angular workspace root and retry.');
process.exit(1);
}
throw e;
} Prevention
- Verify cwd contains angular.json before scripted ng calls
- Set cwd explicitly in task runners/CI steps
- Pin the working directory in IDE run configurations
When it happens
Trigger: Running a workspace-scoped command (e.g. 'ng generate component x' or 'ng build') in a directory without angular.json/.angular.json; commands invoked via npx from an unrelated folder.
Common situations: Opening a terminal in the repo root of a polyglot monorepo where the Angular app is nested; forgetting 'cd' after cloning; global 'ng' invocation from home directory.
Related errors
- This command is not available when running the Angular CLI $
- Could not find ${level} workspace.
- Could not find a ${level} workspace. Are you in a project?
- Cannot determine project for command. This is a multi-projec
- Cannot retrieve cache configuration as workspace is not defi
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/180942dadca977fb.
Report an issue: GitHub.