angular/angular-cli · error · Error
The builder requires a target.
Error message
The builder requires a target.
What it means
The ng-packagr builder's execute needs the project name from context.target to load project metadata (used for cache options, root, tsConfig resolution, etc.). If no target is present, it throws 'The builder requires a target.' This occurs when the packagr builder is invoked programmatically without an architect target rather than via `ng build <lib>`.
Source
Thrown at packages/angular_devkit/build_angular/src/builders/ng-packagr/index.ts:48
return from(
(async () => {
// Purge old build disk cache.
await purgeStaleBuildCache(context);
const root = context.workspaceRoot;
const workspaceRequire = createRequire(root + '/');
const ngPackagePath = workspaceRequire.resolve('ng-packagr');
const packager = (await import(ngPackagePath)).ngPackagr();
packager.forProject(resolve(root, options.project));
if (options.tsConfig) {
packager.withTsConfig(resolve(root, options.tsConfig));
}
const projectName = context.target?.project;
if (!projectName) {
throw new Error('The builder requires a target.');
}
const metadata = await context.getProjectMetadata(projectName);
const { enabled: cacheEnabled, path: cacheDirectory } = normalizeCacheOptions(
metadata,
context.workspaceRoot,
);
const ngPackagrOptions: NgPackagrOptions = {
cacheEnabled,
poll: options.poll,
cacheDirectory: join(cacheDirectory, 'ng-packagr'),
};
return { packager, ngPackagrOptions };
})(),
).pipe(
switchMap(({ packager, ngPackagrOptions }) =>View on GitHub (pinned to bb72145f9a)
Solutions
- Build libraries via `ng build <project>` or `ng run <project>:build` so a target is bound to the context.
- When scheduling programmatically, use scheduleTarget({ project, target }) instead of scheduleBuilder.
- Provide target.project if you construct the builder context manually.
- Define the library's build target in angular.json if missing.
Example fix
// before
await context.scheduleBuilder('@angular-devkit/build-angular:ng-packagr', options);
// after
await context.scheduleTarget({ target: 'build', project: 'my-lib' }, options); Defensive patterns
Strategy: validation
Validate before calling
if (!context.target?.project) {
throw new Error('ng-packagr requires a target: use ng build <lib> or scheduleTarget.');
} Try / catch
try {
await ngBuildLibrary();
} catch (e) {
if (e.message === 'The builder requires a target.') {
console.error('Schedule via ng run <project>:build.');
} else throw e;
} Prevention
- Build libraries with ng run <project>:build, not scheduleBuilder.
- Bind target.project in custom Architect contexts.
- Ensure each library has a build target in angular.json.
- Wrap programmatic builds in a target-presence assertion.
When it happens
Trigger: Scheduling the ng-packagr builder via scheduleBuilder or a hand-rolled Architect context without target.project; invoking ng-packagr's execute directly in tests or tooling.
Common situations: CI scripts building libraries programmatically; wrapper tools around ng-packagr; custom runners that construct build actions without a target descriptor.
Related errors
- The 'karma' builder requires a target to be specified.
- The 'karma' builder requires a target to be specified.
- Cannot find 'main' entrypoint.
- Failed to detect the builder used by the application. Please
- Could not find any routes to prerender.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/5c23356ad718a4a0.
Report an issue: GitHub.