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
- Run tests through a defined target: `ng test` or `ng test <project>:test` with a valid angular.json test target.
- If scheduling programmatically, provide a target: `context.scheduleTarget({ target: 'test', project: 'my-app' }, options)`.
- Verify angular.json contains a test target for the project and the project name is spelled correctly.
- 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
- Prefer scheduleTarget over scheduleBuilder for builder invocations.
- Keep a valid test target in angular.json for every testable project.
- Validate context.target before calling builder APIs in custom tooling.
- Avoid `ng test --project` typos; use tab completion or list projects first.
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
- 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/604fa437106a6a5b.
Report an issue: GitHub.