The @angular-devkit/build-ng-packagr package provides an Angular Build Architect builder for ng-packagr, enabling the compilation and packaging of Angular libraries within the Angular CLI's build system. This package serves as a wrapper around the ng-packagr tool, integrating it seamlessly into Angular workspaces.
⚠️ Deprecation Notice: This package has been deprecated since Angular CLI 10.1. Use the ng-packagr builder from @angular-devkit/build-angular instead.
npm install @angular-devkit/build-ng-packagr@angular-devkit/build-angular instead)import { execute, NgPackagrBuilderOptions } from "@angular-devkit/build-ng-packagr";For the default builder (typically not imported directly):
import builder from "@angular-devkit/build-ng-packagr";For CommonJS:
const { execute, NgPackagrBuilderOptions } = require("@angular-devkit/build-ng-packagr");The package is primarily used as an Angular CLI builder through the angular.json configuration:
{
"projects": {
"my-lib": {
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"project": "projects/my-lib/ng-package.json"
}
}
}
}
}
}Programmatic usage:
import { execute, NgPackagrBuilderOptions } from "@angular-devkit/build-ng-packagr";
import { BuilderContext } from "@angular-devkit/architect";
const options: NgPackagrBuilderOptions = {
project: "ng-package.json",
tsConfig: "tsconfig.lib.json",
watch: false
};
// Execute the builder (requires proper BuilderContext)
const result = execute(options, context);
result.subscribe(buildResult => {
if (buildResult.success) {
console.log("Build completed successfully");
}
});The package is built around the Angular Architect system and consists of:
Main entry point for executing the ng-packagr build process within the Angular Architect system.
/**
* Execute the ng-packagr builder with the provided options
* @deprecated Since 10.1 use executeNgPackagrBuilder from @angular-devkit/build-angular instead
* @param options - Builder configuration options
* @param context - Angular Architect build context
* @returns Observable that emits build result
*/
export function execute(
options: NgPackagrBuilderOptions,
context: BuilderContext
): Observable<BuilderOutput>;Usage Example:
import { execute } from "@angular-devkit/build-ng-packagr";
import { BuilderContext, BuilderOutput } from "@angular-devkit/architect";
// This would typically be called by the Angular CLI
const buildResult: Observable<BuilderOutput> = execute(
{
project: "ng-package.json",
tsConfig: "tsconfig.lib.json",
watch: false
},
context
);
buildResult.subscribe(result => {
console.log(`Build ${result.success ? 'succeeded' : 'failed'}`);
});Interface defining the configuration options for the ng-packagr builder.
/**
* Configuration options for the ng-packagr builder
* @deprecated Since 10.1 use NgPackagrBuilderOptions from @angular-devkit/build-angular instead
*/
interface NgPackagrBuilderOptions {
/**
* Path to the ng-packagr configuration file, relative to workspace root
* This file (typically ng-package.json) contains the ng-packagr build configuration
*/
project: string;
/**
* Path to the TypeScript configuration file, relative to workspace root
* Optional override for the TypeScript compiler configuration
*/
tsConfig?: string;
/**
* Enable watch mode for development
* When true, the builder will watch for file changes and rebuild automatically
* @default false
*/
watch?: boolean;
}The package exports a pre-configured builder that integrates with the Angular CLI.
/**
* Pre-configured Angular Architect builder for ng-packagr
* This is the default export created using createBuilder(execute) and used by the Angular CLI
* when the builder is specified in builders.json/angular.json
*/
export default createBuilder<Record<string, string> & NgPackagrBuilderOptions>(execute);/**
* Type alias for Schema interface (generated from JSON schema)
* @deprecated Since 10.1 use types from @angular-devkit/build-angular instead
*/
export type NgPackagrBuilderOptions = Schema;
/**
* Generated interface from src/build/schema.json
* Contains the configuration options for the ng-packagr builder
*/
interface Schema {
/** The file path for the ng-packagr configuration file, relative to the current workspace */
project: string;
/** The full path for the TypeScript configuration file, relative to the current workspace */
tsConfig?: string;
/** Run build when files change */
watch?: boolean;
}This package relies on external types from Angular DevKit and RxJS:
// From @angular-devkit/architect
interface BuilderContext {
workspaceRoot: string;
// Additional properties from Angular Architect
}
interface BuilderOutput {
success: boolean;
error?: string;
}
// From rxjs
interface Observable<T> {
subscribe(observer: (value: T) => void): Subscription;
// Additional Observable methods
}The builder handles errors through the BuilderOutput interface. Common error scenarios include:
Since this package is deprecated, migrate to the replacement:
// Old (deprecated)
import { execute, NgPackagrBuilderOptions } from "@angular-devkit/build-ng-packagr";
// New (recommended)
import { executeNgPackagrBuilder, NgPackagrBuilderOptions } from "@angular-devkit/build-angular";Update angular.json:
{
"builder": "@angular-devkit/build-angular:ng-packagr"
}