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

The karma builder's `start` needs a builder target (project name) from the architect context to look up project metadata such as sourceRoot. When `context.target` is null or has no project, it cannot proceed and throws this error. This typically means the karma builder was invoked programmatically rather than via an `ng test` target in angular.json.

Source

Thrown at packages/angular_devkit/build_angular/src/builders/karma/browser_builder.ts:53

): AsyncIterable<BuilderOutput> {
  let karmaServer: Server;
  let isCancelled = false;

  return new ReadableStream({
    async start(controller) {
      const [karma, webpackConfig] = await initializeBrowser(
        options,
        context,
        transforms.webpackConfiguration,
      );

      if (isCancelled) {
        return;
      }

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

      const projectMetadata = await context.getProjectMetadata(projectName);
      const sourceRoot = (projectMetadata.sourceRoot ?? projectMetadata.root ?? '') as string;

      if (!options.main) {
        webpackConfig.entry ??= {};
        if (typeof webpackConfig.entry === 'object' && !Array.isArray(webpackConfig.entry)) {
          if (Array.isArray(webpackConfig.entry['main'])) {
            webpackConfig.entry['main'].push(getBuiltInMainFile());
          } else {
            webpackConfig.entry['main'] = [getBuiltInMainFile()];
          }
        }
      }

      webpackConfig.plugins ??= [];
      webpackConfig.plugins.push(

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run tests through a defined target: `ng test` or `ng test <project>:test` with a valid angular.json test target.
  2. If scheduling programmatically, provide a target: `context.scheduleTarget({ target: 'test', project: 'my-app' }, options)`.
  3. Verify angular.json contains a test target for the project and the project name is spelled correctly.
  4. Ensure code constructing the builder context sets `target` before invoking the builder.

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('karma builder must be scheduled with a target (ng test / scheduleTarget).');
}

Try / catch

try {
  await runKarma(context, options);
} catch (e) {
  if (e.message.includes("'karma' builder requires a target")) {
    console.error('Invoke via ng test or scheduleTarget with a project.');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the karma builder's execute/start through the architect API without setting context.target (e.g. `context.scheduleBuilder` misuse or a custom runner that creates a context without a target); invoking the builder outside of a defined test target in angular.json.

Common situations: Custom CI scripts scheduling builders directly; test tooling wrapping the builder API without a target; typos in `ng test --project` or running in a workspace where the target definition was removed.

Related errors


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