
Angular Commands: When to Use Them with Simple Examples
Learn the most useful Angular commands, when to use them, and what each command does. Easy-to-understand examples for beginners and developers.
Angular Commands: When to Use Them
Angular CLI commands help developers to create, run, test, and manage Angular applications faster. Below are the most commonly used Angular commands and when you should use them.
1. Create a New Angular Project
ng new my-appWhen to use:
- Starting a new Angular project.
- Creating a fresh application from scratch.
What it does:
- Creates the project structure.
- Installs required packages.
- Sets up Angular configuration files.
2. Run the Application
ng serveWhen to use:
- During development.
- To see your application in the browser.
What it does:
- Starts a local development server.
- Automatically reloads the page when code changes.
Common variant:
ng serve --openThis automatically opens the browser.
3. Generate a Component
ng generate component home
or
ng g c homeWhen to use:
Creating a new page or UI section.
What it does:
- Creates component files.
- Updates Angular module settings automatically.
4. Generate a Service
ng generate service api
or
ng g s apiWhen to use:
- Handling API calls.
- Sharing data between components.
What it does:
Creates a service file for business logic.
5. Generate a Module
ng generate module admin
or
ng g m adminWhen to use:
- Organizing large applications.
- Separating features into different modules.
6. Generate Routing Module
ng generate module app-routing --flat --module=appWhen to use:
Adding navigation between pages.
What it does:
Creates routing configuration for the application.
7. Build the Application
ng buildWhen to use:
- Before deployment.
- Creating production-ready files.
What it does:
- Compiles the application.
- Creates build files inside the dist folder.
Production build:
ng build --configuration production8. Run Unit Tests
ng testWhen to use:
Checking whether application features work correctly.
What it does:
Runs unit tests using Karma and Jasmine.
9. Run End-to-End Tests
ng e2eWhen to use:
Testing complete user workflows.
What it does:
Simulates real user actions.
10. Check Angular Version
ng versionWhen to use:
- Verifying installed Angular version.
- Debugging compatibility issues.
11. Update Angular
ng updateWhen to use:
Upgrading Angular packages.
What it does:
- Checks available updates.
- Updates dependencies safely.
12. Add Angular Features
ng add package-nameExample:
ng add @angular/materialWhen to use:
Installing Angular-supported libraries.
What it does:
Installs and configures packages automatically.
13. Lint the Project
ng lintWhen to use:
Finding coding issues before deployment.
What it does:
Checks code quality and formatting rules.
14. Create an Interface
ng generate interface user
or
ng g i userWhen to use:
Defining data structures.
Example:
interface User {
id: number;
name: string;
}15. Create a Guard
ng generate guard authWhen to use:
- Protecting routes.
- Restricting access to pages.
Example:
Allow only logged-in users to access the dashboard.
Quick Summary
Command | Use Case |
ng new | Create a new project |
ng serve | Run application locally |
ng g c | Create component |
ng g s | Create service |
ng g m | Create module |
ng build | Create deployment files |
ng test | Run unit tests |
ng version | Check Angular version |
ng update | Update Angular packages |
ng add | Install Angular libraries |
ng lint | Check code quality |
ng g i | Create interface |
ng g guard | Protect routes |
Frequently Asked Questions
What is Angular CLI?
Angular CLI (Command Line Interface) is a tool that helps developers create, manage, build, and maintain Angular applications using simple commands. It reduces manual work and speeds up development.
Which Angular command is used to start a project locally?
The ng serve command is used to run an Angular application on a local development server. You can open the application in your browser and see changes instantly while developing.
How do I create a new component in Angular?
You can create a new component using the ng generate component command or its shorter version ng g c.
What is the difference between ng serve and ng build?
ng serve runs the application locally for development. ng build compiles the application and creates optimized files for deployment.