{"record":{"id":"180942dadca977fb","repo":"angular/angular-cli","slug":"a-workspace-is-required-for-this-command","errorCode":null,"errorMessage":"A workspace is required for this command.","messagePattern":"A workspace is required for this command\\.","errorType":"exception","errorClass":"CommandModuleError","httpStatus":null,"severity":"error","filePath":"packages/angular/cli/src/command-builder/command-module.ts","lineNumber":201,"sourceCode":"    const optionsWithAnalytics = addSchemaOptionsToCommand(\n      localYargs,\n      options,\n      // This should only be done when `--help` is used otherwise default will override options set in angular.json.\n      /* includeDefaultValues= */ this.context.args.options.help,\n    );\n\n    // Record option of analytics.\n    for (const [name, userAnalytics] of optionsWithAnalytics) {\n      this.optionsWithAnalytics.set(name, userAnalytics);\n    }\n\n    return localYargs;\n  }\n\n  protected getWorkspaceOrThrow(): AngularWorkspace {\n    const { workspace } = this.context;\n    if (!workspace) {\n      throw new CommandModuleError('A workspace is required for this command.');\n    }\n\n    return workspace;\n  }\n\n  /**\n   * Flush on an interval (if the event loop is waiting).\n   *\n   * @returns a method that when called will terminate the periodic\n   * flush and call flush one last time.\n   */\n  protected getAnalyticsParameters(\n    options: (Options<T> & OtherOptions) | OtherOptions,\n  ): Partial<Record<EventCustomDimension | EventCustomMetric, string | boolean | number>> {\n    const parameters: Partial<\n      Record<EventCustomDimension | EventCustomMetric, string | boolean | number>\n    > = {};\n","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular/cli/src/command-builder/command-module.ts#L183-L219","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n~ $ ng g c header\n// after\n~ $ cd dev/my-app && ng g c header","handlingStrategy":"validation","validationCode":"import { existsSync } from 'fs';\nif (!existsSync('angular.json') && !existsSync('.angular.json')) {\n  throw new Error('This command must run inside an Angular workspace (angular.json not found).');\n}","typeGuard":null,"tryCatchPattern":"try {\n  execSync('ng generate component x', { stdio: 'inherit' });\n} catch (e) {\n  if (String(e).includes('A workspace is required')) {\n    console.error('cd into your Angular workspace root and retry.');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["angular-cli","workspace","command-scope"],"backgroundTag":"workspace-not-found","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}