The Angular CLI (Command Line Interface) is a powerful tool that helps developers streamline their development workflow with Angular. It provides a variety of commands that make it easy to create, build, test, and maintain Angular applications. In this article, we will explore the essential Angular CLI commands and their usage.

Installation

Before using Angular CLI, you need to install it globally on your system.

Bash
npm install -g @angular/cli

Once installed, you can use the ng command to access the CLI.

Creating a New Angular Application

To create a new Angular application, use the ng new command.

Bash
ng new my-angular-app

This command generates a new Angular project with a default structure and configuration.

Serving the Application

To serve your application locally and start the development server, use the ng serve command.

Bash
ng serve

The application will be available at http://localhost:4200/ by default.

Generating Components, Services, and More

Angular CLI provides several commands to generate different parts of your application. Here are some common ones.

Component

To generate a new component.

Bash
ng generate component my-component

or

Bash
ng g c my-component

Service

To generate a new service.

Bash
ng generate service my-service

or

Bash
ng g s my-service

Module

To generate a new module.

Bash
ng generate module my-module

or

Bash
ng g m my-module

Directive

To generate a new directive.

Bash
ng generate directive my-directive

or

Bash
ng g d my-directive

Pipe

To generate a new pipe.

Bash
ng generate pipe my-pipe

or

Bash
ng g p my-pipe

Class

To generate a new class.

Bash
ng generate class my-class

or

Bash
ng g cl my-class

Building the Application

To build your application for production, use the ng build command.

Bash
ng build

This command compiles the application into an output directory (dist/ by default) and optimizes it for production.

You can also specify different build configurations.

Bash
ng build --configuration=production

Running Tests

Angular CLI makes it easy to run unit tests and end-to-end tests.

Linting

To analyze your code for potential errors and enforce coding standards, use the ng lint command.

Bash
ng lint

This command checks your code against the linting rules defined in your project.

Updating Angular

To update your Angular project and its dependencies, use the ng update command.

Bash
ng update

This command helps keep your project up-to-date with the latest Angular releases and packages.

Other Useful Commands

Conclusion

The Angular CLI is an indispensable tool for Angular developers, offering a wide range of commands to simplify the development process. By mastering these commands, you can significantly enhance your productivity and maintain a clean and efficient workflow. Whether you're creating new components, building your application, running tests, or managing configurations, the Angular CLI provides the necessary tools to streamline your development tasks.