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.
🎯 Installation
Section titled “🎯 Installation”Install the Angular CLI globally using npm:
npm install -g @angular/cliVerify the installation:
ng versionYou should see output showing the Angular CLI version, Node.js version, and package manager:
Angular CLI: 21.0.0Node: 22.12.0Package Manager: npm 10.9.0🚀 Creating a New Project
Section titled “🚀 Creating a New Project”Use ng new to scaffold a complete Angular project:
ng new my-appThe CLI will prompt you for configuration options like stylesheet format and SSR. You can skip prompts with flags:
ng new my-app --style=scss --ssr=false --skip-testsCommon ng new Options
Section titled “Common ng new Options”| Flag | Description |
|---|---|
--style=scss|css|less | Stylesheet preprocessor |
--ssr | Enable server-side rendering |
--skip-tests | Skip generating test files |
--skip-git | Don’t initialize a Git repository |
--prefix=app | Component selector prefix |
--routing | Add routing module |
--package-manager=npm|yarn|pnpm | Package manager to use |
--dry-run | Preview changes without writing files |
🏗️ Code Generation Commands
Section titled “🏗️ Code Generation Commands”The ng generate (or ng g) command scaffolds code using built-in schematics:
Components
Section titled “Components”ng g component features/user-profileThis creates:
src/app/features/user-profile/├── user-profile.component.ts├── user-profile.component.html├── user-profile.component.scss└── user-profile.component.spec.tsThe 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:
ng g component shared/button --inline-template --inline-styleng g component layout/header --skip-testsng g component features/dashboard --flat # No subdirectoryServices
Section titled “Services”ng g service core/services/authGenerates a service with providedIn: 'root':
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root',})export class AuthService {}Other Schematics
Section titled “Other Schematics”ng g directive shared/directives/highlightng g pipe shared/pipes/truncateng g guard core/guards/authng g interceptor core/interceptors/authng g interface models/userng g enum models/roleng g environments🖥️ Development Server
Section titled “🖥️ Development Server”Start the development server with:
ng serveThe app is available at http://localhost:4200 with live reload enabled.
Common ng serve Options
Section titled “Common ng serve Options”ng serve --port 3000 # Custom portng serve --open # Open browser automaticallyng serve --host 0.0.0.0 # Expose to networkng serve --ssl # Enable HTTPSng serve --proxy-config proxy.conf.json # API proxyProxy Configuration
Section titled “Proxy Configuration”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:
ng serve --proxy-config proxy.conf.json📦 Building for Production
Section titled “📦 Building for Production”Build your application for production:
ng buildAngular 21 uses esbuild by default, producing highly optimized bundles. Output goes to dist/my-app/.
Build Options
Section titled “Build Options”ng build --configuration=production # Explicit production configng build --source-map # Include source mapsng build --stats-json # Generate bundle statsng build --base-href /my-app/ # Set base hrefng build --output-hashing all # Cache-busting filenamesAnalyzing Bundle Size
Section titled “Analyzing Bundle Size”Generate a stats file and analyze it:
ng build --stats-jsonnpx esbuild-visualizer --metadata dist/my-app/stats.json --open🧪 Testing with Vitest
Section titled “🧪 Testing with Vitest”Angular 21 uses Vitest as the default test runner:
ng testTest Options
Section titled “Test Options”ng test --watch # Watch mode (default)ng test --no-watch # Single runng test --code-coverage # Generate coverage reportng test --browsers=ChromeHeadless # Headless browser📚 Adding Libraries
Section titled “📚 Adding Libraries”Use ng add to install and configure third-party libraries with their schematics:
ng add @angular/materialng add @angular/pwang add @angular-eslint/schematicsng add @ngrx/storeThe 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.
🔄 Updating Angular
Section titled “🔄 Updating Angular”Keep Angular up to date with:
ng updateThis shows available updates. To apply them:
ng update @angular/core @angular/cliFor major version updates, check the update guide first:
ng update @angular/core@21 @angular/cli@21⚙️ Configuration — angular.json
Section titled “⚙️ Configuration — angular.json”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" } } } } }}Key Configuration Areas
Section titled “Key Configuration Areas”budgets— Set size limits to catch bundle bloat earlyfileReplacements— Swap environment files per configurationassets— Static files copied to the build outputstyles/scripts— Global styles and scriptsoutputPath— Where built files are written
🏭 Custom Schematics (Brief Overview)
Section titled “🏭 Custom Schematics (Brief Overview)”You can create reusable code generators for your team:
npm install -g @angular-devkit/schematics-clischematics blank --name=my-schematicsCustom schematics are useful for enforcing team patterns — generating feature modules with a consistent structure, creating boilerplate for state management, or scaffolding API service layers.
📋 CLI Command Reference
Section titled “📋 CLI Command Reference”| Command | Description |
|---|---|
ng new | Create a new workspace |
ng generate | Generate code from schematics |
ng serve | Start dev server |
ng build | Build for production |
ng test | Run unit tests |
ng lint | Run linter |
ng add | Add a library with schematics |
ng update | Update Angular packages |
ng deploy | Deploy (with adapter) |
ng analytics | Configure CLI analytics |
ng cache | Manage CLI disk cache |
ng completion | Set up shell autocompletion |
Run ng help or ng <command> --help for full documentation on any command.