angular/angular-cli · error · Error

The 'karma' builder requires a target to be specified.

Error message

The 'karma' builder requires a target to be specified.

What it means

getBaseKarmaOptions (invoked from karmaOptions) derives the project name from the builder context's target to build the default karma configuration. Without a target project, it cannot name the browser instance or resolve project settings, so it throws. Like error 153, this indicates the karma builder was invoked without an architect target.

Source

Thrown at packages/angular_devkit/build_angular/src/builders/karma/index.ts:92

    const { execute } = await import('./browser_builder');

    yield* execute(options, context, karmaOptions, transforms);
  }
}

function getBaseKarmaOptions(
  options: KarmaBuilderOptions,
  context: BuilderContext,
): KarmaConfigOptions {
  let singleRun: boolean | undefined;
  if (options.watch !== undefined) {
    singleRun = !options.watch;
  }

  // Determine project name from builder context target
  const projectName = context.target?.project;
  if (!projectName) {
    throw new Error(`The 'karma' builder requires a target to be specified.`);
  }

  const karmaOptions: KarmaConfigOptions = options.karmaConfig
    ? {}
    : getBuiltInKarmaConfig(context.workspaceRoot, projectName);

  karmaOptions.singleRun = singleRun;

  // Workaround https://github.com/angular/angular-cli/issues/28271, by clearing context by default
  // for single run executions. Not clearing context for multi-run (watched) builds allows the
  // Jasmine Spec Runner to be visible in the browser after test execution.
  karmaOptions.client ??= {};
  karmaOptions.client.clearContext ??= singleRun ?? false; // `singleRun` defaults to `false` per Karma docs.

  // Convert browsers from a string to an array
  if (typeof options.browsers === 'string' && options.browsers) {
    karmaOptions.browsers = options.browsers.split(',');
  } else if (options.browsers === false) {

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run via `ng test` (or `ng test <project>:test`) so a target is present in the context.
  2. When scheduling programmatically use scheduleTarget with a valid project and target name.
  3. If calling karmaOptions/getBaseKarmaOptions yourself, construct a context with target: { project: '<name>', target: 'test' }.
  4. Restore or define a test target for the project in angular.json.

Example fix

// before
await context.scheduleBuilder('@angular-devkit/build-angular:karma', options);
// after
await context.scheduleTarget({ target: 'test', project: 'my-app' }, options);
Defensive patterns

Strategy: validation

Validate before calling

if (!context.target?.project) {
  throw new Error('Provide a target project: ng test or scheduleTarget({ project, target: "test" }).');
}

Try / catch

try {
  const opts = karmaOptions(context, options);
} catch (e) {
  if (e.message.includes("'karma' builder requires a target")) {
    console.error('Bind a target before computing karma options.');
  } else throw e;
}

Prevention

When it happens

Trigger: Invoking the karma builder programmatically with a context whose `target` is undefined/null; custom tooling that calls karmaOptions/getBaseKarmaOptions directly without a target; running a scheduled builder without a project.

Common situations: Custom test runners scheduling builders via `scheduleBuilder` instead of `scheduleTarget`; CI harnesses constructing Architect contexts manually; workspaces where angular.json test targets were deleted.

Related errors


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