Angular CLI Commands
The Angular CLI is your best friend. Master these commands and work 10x faster.
The Angular CLI (ng) is a command-line tool that automates many development tasks. The most powerful command is ng generate (shortcut: ng g).
# Generate a component (shortcut: ng g c)
ng g c hello
ng generate component hello # Same thing, longer form
What ng generate can create:
ng g c name— Component (most common)ng g s name— Serviceng g p name— Pipeng g g name— Guardng g d name— Directiveng g m name— Module (rarely needed with standalone)ng g i name— Interfaceng g cl name— Classng g e name— Enum
Hyderabad Tip: CLI = assistant — bole toh component bana de, service bana de. Jaise chai hotel mein "Ek chai" bolte ho, waise terminal mein "ng g c hello" bolo!
The ng g c command has many options to customize what gets generated. Let's explore them:
# Basic component generation
ng g c components/hello
# Creates: components/hello/hello.component.ts
# components/hello/hello.component.html
# components/hello/hello.component.scss
# components/hello/hello.component.spec.ts
# No separate folder (flat)
ng g c components/hello --flat
# Creates files directly in components/ without a subfolder
# Skip test file
ng g c hello --skip-tests
# No .spec.ts file (useful for quick prototypes)
# Inline template (no separate .html file)
ng g c hello --inline-template
# Template is in the .ts file using template property
# Inline style (no separate .scss file)
ng g c hello --inline-style
# Styles are in the .ts file using styles array
# Shortcut: ng g c = ng generate component, dono same hai!
Pro tip: If you're creating a component and you know you won't need unit tests for it, add --skip-tests. You can always add tests later. For production components, always include tests.
CLI can generate more than just components. Let's explore other generators:
# Generate a service
ng g s services/biryani
# Creates: services/biryani.service.ts
# Services are injectable classes that hold shared logic
# Generate a pipe
ng g p pipes/currency-format
# Creates: pipes/currency-format.pipe.ts
# Pipes transform data in templates (date, currency, etc.)
# Generate a guard
ng g g guards/auth
# Creates: guards/auth.guard.ts
# Guards protect routes from unauthorized access
# Generate a directive
ng g d directives/highlight
# Creates: directives/highlight.directive.ts
# Directives add behavior to existing elements
# Generate an interface
ng g i models/user
# Creates: models/user.ts
# Interfaces define the shape of data (TypeScript only)
Each generator creates the correct file with the right decorator and boilerplate code. You just fill in the logic!
Two of the most important commands you'll use daily:
ng serve — Development Server
# Start dev server (default port 4200)
ng serve
# Open browser automatically
ng serve --open # or: ng serve -o
# Custom port
ng serve --port 4300
# No browser open, just start server
ng serve --no-open
ng build — Production Build
# Development build (larger files, includes sourcemaps)
ng build
# Production build (optimized, minified, smaller)
ng build --configuration production
# Watch mode (rebuild on changes)
ng build --watch
Key difference: ng serve is like cooking in a test kitchen — quick iterations, hot reload, no need to optimize. ng build --configuration production is like packaging biryani for delivery — optimized, minified, and ready for customers.
Here are other useful CLI commands every Angular developer should know:
# Check Angular version
ng version
# Shows Angular CLI, Angular Core, TypeScript, and RxJS versions
# Update Angular to latest version
ng update @angular/core @angular/cli
# Updates Angular packages to the latest compatible version
# Run linter (code quality check)
ng lint
# Checks your code for style and quality issues
# Run unit tests
ng test
# Opens Karma test runner and runs all .spec.ts files
# Run end-to-end tests
ng e2e
# Runs end-to-end tests (requires Protractor or Cypress)
# Check if there's a new Angular CLI version
ng version
# Shows current version and whether an update is available
# Toggle analytics
ng analytics
g ng analytics off # Turn off analytics collection
Pro tip: Run ng --help anytime to see all available commands. The CLI has extensive built-in documentation.
Key Takeaways
- ✅ ng g c = ng generate component — creates component with all files
- ✅ Use --flat, --skip-tests, --inline-template, --inline-style to customize
- ✅ ng serve = development server with hot reload
- ✅ ng build --configuration production = optimized production build
- ✅ ng version, ng update, ng lint, ng test, ng e2e are other essential commands
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login