Table of contents
1.
What is Angular CLI?
2.
Installing Angular CLI
3.
Basic Workflow
4.
Workspaces and Project Files
5.
CLI Command Reference
5.1.
ng add Command
5.1.1.
Syntax
5.1.2.
Parameter Explanation
5.1.3.
Example
5.2.
ng build Command
5.2.1.
Syntax
5.2.2.
Parameter Explanation
5.2.3.
Example
5.3.
ng config Command
5.3.1.
Syntax
5.3.2.
Parameter Explanation
5.3.3.
Example
5.4.
ng doc Command
5.4.1.
Syntax
5.4.2.
Parameter Explanation
5.4.3.
Example
5.5.
ng e2e Command
5.5.1.
Syntax
5.5.2.
Parameter Explanation
5.5.3.
Example
5.6.
ng generate Command
5.6.1.
Syntax:
5.6.2.
Parameter Explanation
5.6.3.
Example
6.
Frequently Asked Questions
6.1.
What is Angular CLI used for?
6.2.
How do I update Angular CLI to the latest version?
6.3.
Can I use Angular CLI without installing Node.js?
7.
Conclusion
Last Updated: Feb 16, 2025
Medium

Angular CLI Commands

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

What is Angular CLI?

Angular CLI (Command Line Interface) is a tool that helps developers create, build, and manage Angular applications efficiently. It provides commands for generating components, services, and modules, simplifying project development. With Angular CLI, developers can also serve applications, build for production, and run tests easily.

Angular CLI Commands

In this article, you will learn about the most important Angular CLI commands, their syntax, and how to use them effectively in your projects.

Installing Angular CLI

Before you can start using Angular CLI, you need to install it on your system. The installation process is straightforward & requires Node.js & npm (Node Package Manager) to be installed on your machine. If you don't have Node.js installed, you can download it from the official website (https://nodejs.org/).

Once Node.js is installed, open your terminal or command prompt & run the following command to install Angular CLI globally:

npm install -g @angular/cli

 

This command installs the latest version of Angular CLI globally on your system, making it accessible from any directory. The `-g` flag ensures that the package is installed globally.
 

After the installation is complete, you can verify it by checking the version of Angular CLI installed:

ng version

 

This command displays the installed version of Angular CLI along with other details like Node.js version & Angular core packages. If you see the version number, it means Angular CLI is installed successfully.

Basic Workflow

Once Angular CLI is installed, you can start using it to create & manage Angular projects. The basic workflow involves creating a new project, generating components, services, & other Angular elements, running the development server, & building the project for production. Let’s discuss this in a step by step manner.

1. Creating a New Angular Project

To create a new Angular project, use the `ng new` command followed by the name of your project. For example, if you want to create a project called `my-app`, run the following command:

ng new my-app

 

This command will prompt you with a few questions, such as whether you want to include Angular routing & which stylesheet format you prefer (CSS, SCSS, etc.). Answer these questions based on your project requirements.

Once the setup is complete, Angular CLI will generate a new folder with the name `my-app` containing all the necessary files & folders for your Angular project.
 

2. Navigating to the Project Directory

After creating the project, navigate to the project directory using the `cd` command:

cd my-app

 

Now, you’re inside the project folder & ready to start working on it.

 

3. Running the Development Server

To see your Angular application in action, you need to start the development server. Use the following command:

ng serve

 

This command compiles the application & starts a local development server. By default, the application will be available at `http://localhost:4200`. Open your browser & visit this URL to see your Angular app running.

If you want the server to automatically reload when you make changes to your code, use the `--open` flag:

ng serve --open

 

This will open the application in your default browser & automatically reload it whenever you save changes.

 

4. Generating Components, Services, & More

Angular CLI makes it easy to generate components, services, directives, & other Angular elements. For example, to generate a new component called `header`, use the following command:

ng generate component header

 

This command creates a new folder named `header` inside the `src/app` directory & generates four files:

  • `header.component.ts` (TypeScript file for the component logic)
     
  • `header.component.html` (HTML template for the component)
     
  • `header.component.css` (CSS file for styling)
     
  • `header.component.spec.ts` (Test file for the component)

 

Similarly, you can generate services, directives, & other elements. For example, to generate a service called `data`, use:

ng generate service data

 

This creates a service file named `data.service.ts` in the `src/app` directory.

 

5. Building the Project for Production

When you’re ready to deploy your Angular application, you need to build it for production. Use the following command:

ng build --prod

 

This command compiles the application & optimizes it for production. The output files are stored in the `dist/my-app` folder. You can deploy these files to a web server or hosting platform.

Workspaces and Project Files

Angular CLI organizes applications into workspaces. A workspace can contain multiple projects, including applications and libraries. Important files in an Angular project include:

  • angular.json – Configuration settings for the Angular CLI
     
  • package.json – Lists dependencies and scripts
     
  • src/ – Contains the main application code
     
  • node_modules/ – Contains installed dependencies

CLI Command Reference

ng add Command

The ng add command is used to add external packages to an Angular project.

Syntax

ng add <package-name>

Parameter Explanation

  • <package-name>: Name of the package to be added.

Example

ng add @angular/material

 

This command installs Angular Material in your project and updates configuration files.

ng build Command

The ng build command compiles an Angular project into an output directory.

Syntax

ng build [options]

Parameter Explanation

  • [options]: Optional parameters for build configuration.

Example

ng build --prod

 

This command creates an optimized production build.

ng config Command

The ng config command is used to set or retrieve configuration values.

Syntax

ng config <key> <value>

Parameter Explanation

  • <key>: Configuration key.
     
  • <value>: New value for the key.

Example

ng config cli.defaultCollection @angular/schematics

 

This sets the default collection for generating components.

ng doc Command

The ng doc command opens the official Angular documentation for a specified topic.

Syntax

ng doc <keyword>

Parameter Explanation

  • <keyword>: Angular topic to search for.

Example

ng doc component

 

This opens the Angular documentation page for components.

ng e2e Command

The ng e2e command is used to run end-to-end tests for an application.

Syntax

ng e2e [options]

Parameter Explanation

  • [options]: Optional parameters for test configuration.

Example

ng e2e --port 4300

 

This runs end-to-end tests on port 4300.

ng generate Command

The ng generate command is used to create Angular components, services, and other elements.

Syntax:

ng generate <schematic> <name>

Parameter Explanation

  • <schematic>: Type of element to generate (component, service, module, etc.).
     
  • <name>: Name of the element.

Example

ng generate component my-component

 

This creates a new component named my-component.

Frequently Asked Questions

What is Angular CLI used for?

Angular CLI is used to automate tasks such as creating components, building applications, and running tests in Angular projects.

How do I update Angular CLI to the latest version?

Run the following command: npm install -g @angular/cli@latest

Can I use Angular CLI without installing Node.js?

No, Angular CLI requires Node.js and npm to be installed.

Conclusion

In this article, we covered essential Angular CLI commands, which help in creating, developing, and managing Angular applications efficiently. Commands like ng new, ng serve, ng generate, and ng build simplify tasks such as project setup, component creation, and deployment. Understanding these commands enhances development speed, improves project structure, and ensures better maintainability in Angular applications.

Live masterclass