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
- Run via `ng test` (or `ng test <project>:test`) so a target is present in the context.
- When scheduling programmatically use scheduleTarget with a valid project and target name.
- If calling karmaOptions/getBaseKarmaOptions yourself, construct a context with target: { project: '<name>', target: 'test' }.
- 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
- Run tests through defined angular.json targets.
- Use scheduleTarget programmatically.
- Check target presence before invoking karmaOptions in wrappers.
- Don't delete test targets when refactoring angular.json.
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
- The 'karma' builder requires a target to be specified.
- Cannot find 'main' entrypoint.
- Failed to detect the builder used by the application. Please
- The builder requires a target.
- No "test" target found for project "${options.project}". A "
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/70d8a6584cc9c259.
Report an issue: GitHub.