angular/angular-cli · error · CommandModuleError

This command is not available when running the Angular CLI $

Error message

This command is not available when running the Angular CLI ${workspace ? 'inside' : 'outside'} a workspace.

What it means

Each Angular CLI command declares a scope: CommandScope.In (only inside a workspace) or CommandScope.Out (only outside). addCommandModuleToYargs enforces this when registering commands and throws this error indicating whether you were 'inside' or 'outside' a workspace.

Source

Thrown at packages/angular/cli/src/command-builder/utilities/command.ts:53

  const describe = jsonHelp ? cmd.fullDescribe : cmd.describe;

  context.yargsInstance.command({
    command: cmd.command,
    aliases: cmd.aliases,
    describe:
      // We cannot add custom fields in help, such as long command description which is used in AIO.
      // Therefore, we get around this by adding a complex object as a string which we later parse when generating the help files.
      typeof describe === 'object' ? JSON.stringify(describe) : describe,
    deprecated: cmd.deprecated,
    builder: (argv) => {
      // Skip scope validation when running with '--json-help' since it's easier to generate the output for all commands this way.
      const isInvalidScope =
        !jsonHelp &&
        ((cmd.scope === CommandScope.In && !workspace) ||
          (cmd.scope === CommandScope.Out && workspace));

      if (isInvalidScope) {
        throw new CommandModuleError(
          `This command is not available when running the Angular CLI ${
            workspace ? 'inside' : 'outside'
          } a workspace.`,
        );
      }

      return cmd.builder(argv) as Argv;
    },
    handler: (args) => cmd.handler(args),
  });
}

View on GitHub (pinned to bb72145f9a)

Solutions

  1. cd into (or out of) the appropriate directory: workspace commands need angular.json present
  2. Run 'ng new my-app' from outside a workspace, then cd into it
  3. Check 'pwd' — IDE terminals often open at the workspace root of a monorepo, not the app
  4. Use the correct command variant for your location, or wrap in npm scripts executed at the right cwd

Example fix

// before (inside a workspace)
ng new other-app
// after
cd .. && ng new other-app
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'fs';
import { CommandScope } from '@angular/cli/src/command-builder/command-builder';

function scopeMatches(cmdScope: 'in' | 'out'): boolean {
  const inWorkspace = existsSync('angular.json');
  return cmdScope === 'in' ? inWorkspace : !inWorkspace;
}
if (!scopeMatches(cmd.scope)) process.chdir(correctDir);

Try / catch

try {
  await runCommand(args);
} catch (e) {
  const m = /not available when running the Angular CLI (inside|outside) a workspace/.exec(String(e));
  if (m) {
    console.error(`Wrong location: you are ${m[1]} a workspace for this command.`);
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: Running an inside-only command (e.g. 'ng serve', 'ng generate') outside a workspace, or an outside-only command (e.g. 'ng new', 'ng add' historically) inside a workspace. Suppressed when --json-help is used.

Common situations: 'ng serve' in a plain Node repo; 'ng new' executed inside an existing Angular workspace; running global 'ng' from $HOME; IDE terminals defaulting to the wrong directory.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/39c361d13030044f. Report an issue: GitHub.