Skip to content

Angular CLI ⚙️

The Angular CLI (@angular/cli) is the official command-line tool for creating, developing, building, and maintaining Angular applications. It handles project scaffolding, code generation, development serving, production builds, testing, and much more — all with sensible defaults and powerful customization.

Install the Angular CLI globally using npm:

Terminal window
npm install -g @angular/cli

Verify the installation:

Terminal window
ng version

You should see output showing the Angular CLI version, Node.js version, and package manager:

Angular CLI: 21.0.0
Node: 22.12.0
Package Manager: npm 10.9.0

Use ng new to scaffold a complete Angular project:

Terminal window
ng new my-app

The CLI will prompt you for configuration options like stylesheet format and SSR. You can skip prompts with flags:

Terminal window
ng new my-app --style=scss --ssr=false --skip-tests
FlagDescription
--style=scss|css|lessStylesheet preprocessor
--ssrEnable server-side rendering
--skip-testsSkip generating test files
--skip-gitDon’t initialize a Git repository
--prefix=appComponent selector prefix
--routingAdd routing module
--package-manager=npm|yarn|pnpmPackage manager to use
--dry-runPreview changes without writing files

The ng generate (or ng g) command scaffolds code using built-in schematics:

Terminal window
ng g component features/user-profile

This creates:

src/app/features/user-profile/
├── user-profile.component.ts
├── user-profile.component.html
├── user-profile.component.scss
└── user-profile.component.spec.ts

The generated component uses standalone and OnPush change detection by default:

import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrl: './user-profile.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserProfileComponent {}

Useful generation flags:

Terminal window
ng g component shared/button --inline-template --inline-style
ng g component layout/header --skip-tests
ng g component features/dashboard --flat # No subdirectory
Terminal window
ng g service core/services/auth

Generates a service with providedIn: 'root':

import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class AuthService {}
Terminal window
ng g directive shared/directives/highlight
ng g pipe shared/pipes/truncate
ng g guard core/guards/auth
ng g interceptor core/interceptors/auth
ng g interface models/user
ng g enum models/role
ng g environments

Start the development server with:

Terminal window
ng serve

The app is available at http://localhost:4200 with live reload enabled.

Terminal window
ng serve --port 3000 # Custom port
ng serve --open # Open browser automatically
ng serve --host 0.0.0.0 # Expose to network
ng serve --ssl # Enable HTTPS
ng serve --proxy-config proxy.conf.json # API proxy

Create proxy.conf.json to proxy API requests during development:

{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/api": ""
}
}
}

Then serve with the proxy:

Terminal window
ng serve --proxy-config proxy.conf.json

Build your application for production:

Terminal window
ng build

Angular 21 uses esbuild by default, producing highly optimized bundles. Output goes to dist/my-app/.

Terminal window
ng build --configuration=production # Explicit production config
ng build --source-map # Include source maps
ng build --stats-json # Generate bundle stats
ng build --base-href /my-app/ # Set base href
ng build --output-hashing all # Cache-busting filenames

Generate a stats file and analyze it:

Terminal window
ng build --stats-json
npx esbuild-visualizer --metadata dist/my-app/stats.json --open

Angular 21 uses Vitest as the default test runner:

Terminal window
ng test
Terminal window
ng test --watch # Watch mode (default)
ng test --no-watch # Single run
ng test --code-coverage # Generate coverage report
ng test --browsers=ChromeHeadless # Headless browser

Use ng add to install and configure third-party libraries with their schematics:

Terminal window
ng add @angular/material
ng add @angular/pwa
ng add @angular-eslint/schematics
ng add @ngrx/store

The ng add command runs the library’s schematic, which can modify angular.json, add imports, update styles, and more — beyond what npm install alone does.

Keep Angular up to date with:

Terminal window
ng update

This shows available updates. To apply them:

Terminal window
ng update @angular/core @angular/cli

For major version updates, check the update guide first:

Terminal window
ng update @angular/core@21 @angular/cli@21

The angular.json file is the central configuration for your Angular workspace. Key sections:

{
"projects": {
"my-app": {
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"outputPath": "dist/my-app",
"index": "src/index.html",
"browser": "src/main.ts",
"styles": ["src/styles.scss"],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kB",
"maximumError": "1MB"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
}
},
"serve": {
"builder": "@angular/build:dev-server",
"options": {
"proxyConfig": "proxy.conf.json"
}
}
}
}
}
}
  • budgets — Set size limits to catch bundle bloat early
  • fileReplacements — Swap environment files per configuration
  • assets — Static files copied to the build output
  • styles / scripts — Global styles and scripts
  • outputPath — Where built files are written

You can create reusable code generators for your team:

Terminal window
npm install -g @angular-devkit/schematics-cli
schematics blank --name=my-schematics

Custom schematics are useful for enforcing team patterns — generating feature modules with a consistent structure, creating boilerplate for state management, or scaffolding API service layers.

CommandDescription
ng newCreate a new workspace
ng generateGenerate code from schematics
ng serveStart dev server
ng buildBuild for production
ng testRun unit tests
ng lintRun linter
ng addAdd a library with schematics
ng updateUpdate Angular packages
ng deployDeploy (with adapter)
ng analyticsConfigure CLI analytics
ng cacheManage CLI disk cache
ng completionSet up shell autocompletion

Run ng help or ng <command> --help for full documentation on any command.