Angular CLI (Command Line Interface) is a powerful tool that simplifies the process of initializing, developing, and maintaining Angular applications. It provides a number of helpful commands for your workflow, including creating new projects, generating application features, running your application during development, running tests, and more. In this tutorial, we will guide you through the process of using Angular CLI for your Angular projects.
Table of Contents
ToggleInstalling Angular CLI
Angular CLI is available as an npm package. To install it, you need to have Node.js and npm (Node Package Manager) installed on your machine. You can install Angular CLI globally using the following command:
npm install -g @angular/cli
Creating a New Angular Project
To create a new Angular project, you can use the ng new
command followed by the name of your new project. For example:
ng new my-angular-app
This command creates a new directory with the specified project name, sets up the new Angular application, and installs all the necessary npm packages. The new application will follow the best practices from the official Angular style guide, and it will also include configuration for unit testing and end-to-end testing.
Serving the Application
To run your Angular application during development, you can use the ng serve
command. This command builds the application and starts a web server.
ng serve
By default, the application will be available at http://localhost:4200/
. The ng serve
command also watches your files and rebuilds the application whenever you save changes.
Generating Application Features
Angular CLI provides the ng generate
(or ng g
for short) command to generate application features such as components, services, routes, and more. For example, to generate a new component, you can use the following command:
ng generate component my-component
This command generates a new component with the specified name and updates the application module to declare the new component.
Running Tests
Angular CLI includes commands for running unit tests and end-to-end tests. To run unit tests, you can use the ng test
command. This command uses the Karma test runner to run your tests.
ng test
To run end-to-end tests, you can use the ng e2e
command. This command uses the Protractor end-to-end test framework to run your tests.
ng e2e
Building the Application for Production
To build your Angular application for production, you can use the ng build
command with the --prod
flag. This command creates a dist/
directory with all the files needed to deploy your application.
ng build --prod
Conclusion
Angular CLI is a powerful tool that simplifies the process of working with Angular projects. It provides a number of helpful commands for creating new projects, generating application features, running your application during development, running tests, and building your application for production. By understanding how to use Angular CLI, you can streamline your Angular development workflow and ensure that your projects follow the best practices recommended by the Angular team.