The Angular CLI provides comprehensive package management utilities supporting multiple package managers (npm, yarn, pnpm, bun, cnpm) with unified APIs for installation, dependency management, and package operations.
Core class providing package management operations with automatic package manager detection and configuration.
/**
* Utility class for package manager operations
*/
class PackageManagerUtils {
/** Name of the package manager being used */
readonly name: PackageManager;
/** Version of the package manager */
readonly version: string;
/**
* Install a specific package
* @param packageName - Name of package to install (e.g., "@angular/core@latest")
* @param options - Installation options
* @returns Promise that resolves when installation completes
*/
install(packageName: string, options?: InstallOptions): Promise<void>;
/**
* Install all dependencies from package.json
* @param options - Installation options
* @returns Promise that resolves when installation completes
*/
installAll(options?: InstallOptions): Promise<void>;
/**
* Install package to temporary directory
* @param packageName - Name of package to install
* @returns Promise resolving to temporary installation path
*/
installTemp(packageName: string): Promise<string>;
}Usage Examples:
import { PackageManagerUtils, PackageManager } from "@angular/cli";
// Create utilities instance for specific package manager
const npmUtils = new PackageManagerUtils({
packageManager: PackageManager.npm,
workspaceRoot: process.cwd()
});
// Install specific package
await npmUtils.install("@angular/material@latest", {
save: true
});
// Install dev dependency
await npmUtils.install("@types/node", {
saveDev: true
});
// Install all dependencies
await npmUtils.installAll();
// Install to temporary location for inspection
const tempPath = await npmUtils.installTemp("lodash@4.17.21");
console.log(`Package installed to: ${tempPath}`);Functions for detecting and configuring package managers in Angular workspaces.
/**
* Package manager enumeration
*/
enum PackageManager {
npm = 'npm',
cnpm = 'cnpm',
yarn = 'yarn',
pnpm = 'pnpm',
bun = 'bun'
}
/**
* Get configured package manager for workspace
* @param workspace - Angular workspace instance
* @returns Detected or configured package manager
*/
function getConfiguredPackageManager(workspace?: AngularWorkspace): PackageManager;
/**
* Detect package manager from lock files and configuration
* @param workspaceRoot - Root directory to search
* @returns Detected package manager or npm as default
*/
function detectPackageManager(workspaceRoot: string): PackageManager;Usage Examples:
import {
getConfiguredPackageManager,
detectPackageManager,
getWorkspace
} from "@angular/cli";
// Get configured package manager from workspace
const workspace = getWorkspace('local');
const configuredPM = getConfiguredPackageManager(workspace);
console.log(`Configured package manager: ${configuredPM}`);
// Detect package manager from files
const detectedPM = detectPackageManager(process.cwd());
console.log(`Detected package manager: ${detectedPM}`);
// Detection logic checks for:
// - pnpm-lock.yaml → pnpm
// - yarn.lock → yarn
// - bun.lockb → bun
// - package-lock.json → npm
// - Default → npmFunctions for fetching and analyzing package information from registries.
/**
* Fetch package metadata from registry
* @param packageName - Name of package to fetch
* @param options - Fetch options including registry URL
* @returns Promise resolving to package metadata
*/
function fetchPackageMetadata(
packageName: string,
options?: {
registry?: string;
timeout?: number;
}
): Promise<PackageMetadata>;
/**
* Fetch package manifest from registry
* @param packageName - Name of package to fetch
* @param version - Specific version to fetch (optional)
* @returns Promise resolving to package manifest
*/
function fetchPackageManifest(
packageName: string,
version?: string
): Promise<PackageManifest>;Usage Examples:
import { fetchPackageMetadata, fetchPackageManifest } from "@angular/cli";
// Fetch comprehensive package metadata
const metadata = await fetchPackageMetadata("@angular/core");
console.log(`Latest version: ${metadata["dist-tags"].latest}`);
console.log(`Available versions: ${Object.keys(metadata.versions).length}`);
// Fetch specific version manifest
const manifest = await fetchPackageManifest("@angular/core", "18.0.0");
console.log(`Dependencies: ${Object.keys(manifest.dependencies || {}).length}`);
console.log(`Peer dependencies: ${Object.keys(manifest.peerDependencies || {}).length}`);Interfaces for package manager context and configuration.
/**
* Context for package manager operations
*/
interface PackageManagerUtilsContext {
/** Workspace root directory */
workspaceRoot: string;
/** Package manager to use */
packageManager: PackageManager;
/** Registry URL (optional) */
registry?: string;
/** Force package manager even if lockfile suggests different one */
force?: boolean;
}
/**
* Options for package installation
*/
interface InstallOptions {
/** Save to dependencies */
save?: boolean;
/** Save to devDependencies */
saveDev?: boolean;
/** Save to peerDependencies */
savePeer?: boolean;
/** Save to optionalDependencies */
saveOptional?: boolean;
/** Install globally */
global?: boolean;
/** Skip running install scripts */
ignoreScripts?: boolean;
/** Use exact version (no version range) */
saveExact?: boolean;
/** Registry to install from */
registry?: string;
/** Package manager specific arguments */
extraArgs?: string[];
}Full support for npm with all standard commands and options.
// NPM-specific features
const npmUtils = new PackageManagerUtils({
packageManager: PackageManager.npm,
workspaceRoot: process.cwd()
});
// Uses npm commands: install, ci, uninstall, update, audit
await npmUtils.install("package-name");Comprehensive Yarn Classic and Yarn Berry support with workspace awareness.
// Yarn-specific features
const yarnUtils = new PackageManagerUtils({
packageManager: PackageManager.yarn,
workspaceRoot: process.cwd()
});
// Uses yarn commands: add, install, remove, upgrade, audit
// Automatically detects Yarn Classic vs Yarn Berry
await yarnUtils.install("package-name");Full pnpm support with efficient symlink-based node_modules structure.
// PNPM-specific features
const pnpmUtils = new PackageManagerUtils({
packageManager: PackageManager.pnpm,
workspaceRoot: process.cwd()
});
// Uses pnpm commands: add, install, remove, update, audit
// Supports pnpm workspace features
await pnpmUtils.install("package-name");Experimental Bun package manager support with high-performance operations.
// Bun-specific features
const bunUtils = new PackageManagerUtils({
packageManager: PackageManager.bun,
workspaceRoot: process.cwd()
});
// Uses bun commands: add, install, remove, update
// Leverages Bun's fast JavaScript runtime
await bunUtils.install("package-name");Support for CNPM (China NPM mirror) with localized registry access.
// CNPM-specific features
const cnpmUtils = new PackageManagerUtils({
packageManager: PackageManager.cnpm,
workspaceRoot: process.cwd()
});
// Uses cnpm commands with China registry by default
await cnpmUtils.install("package-name");import { PackageManagerUtils, PackageManager } from "@angular/cli";
const utils = new PackageManagerUtils({
packageManager: PackageManager.npm,
workspaceRoot: process.cwd(),
registry: "https://registry.npmjs.org/"
});
// Install from custom registry
await utils.install("@company/private-package", {
registry: "https://npm.company.com/"
});import { PackageManagerUtils } from "@angular/cli";
async function installDependencies(packages: string[]) {
const utils = new PackageManagerUtils({
packageManager: PackageManager.npm,
workspaceRoot: process.cwd()
});
for (const pkg of packages) {
try {
await utils.install(pkg, { save: true });
console.log(`✓ Installed ${pkg}`);
} catch (error) {
console.error(`✗ Failed to install ${pkg}: ${error.message}`);
}
}
}
// Install multiple packages
await installDependencies([
"@angular/material",
"@angular/cdk",
"@angular/animations"
]);import {
PackageManagerUtils,
detectPackageManager,
PackageManager
} from "@angular/cli";
async function migratePackageManager(
fromPM: PackageManager,
toPM: PackageManager
) {
const workspaceRoot = process.cwd();
// Remove old lock files
if (fromPM === PackageManager.npm) {
// Remove package-lock.json
} else if (fromPM === PackageManager.yarn) {
// Remove yarn.lock
}
// Install with new package manager
const utils = new PackageManagerUtils({
packageManager: toPM,
workspaceRoot
});
await utils.installAll();
console.log(`Migrated from ${fromPM} to ${toPM}`);
}/**
* Package metadata from registry
*/
interface PackageMetadata {
name: string;
description: string;
"dist-tags": Record<string, string>;
versions: Record<string, PackageManifest>;
time: Record<string, string>;
maintainers: Array<{
name: string;
email: string;
}>;
keywords?: string[];
homepage?: string;
repository?: {
type: string;
url: string;
};
}
/**
* Package manifest (package.json contents)
*/
interface PackageManifest {
name: string;
version: string;
description?: string;
main?: string;
types?: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
scripts?: Record<string, string>;
keywords?: string[];
author?: string | {
name: string;
email?: string;
url?: string;
};
license?: string;
repository?: string | {
type: string;
url: string;
};
bugs?: string | {
url: string;
email?: string;
};
homepage?: string;
}