Angular CLI - Command line interface tool
Introduction to Angular CLI
Angular CLI is a tool used to create, run, build, and manage Angular applications using commands.
Angular CLI is a tool that helps developers develop Angular applications.
In development, Angular CLI helps with:
- Creating an application
- Running the application
- Building the application
- Testing the application
- Deploying the application
So, in short, Angular CLI helps manage the complete Angular application lifecycle.
CLI is Important
Angular CLI is used in almost every step of Angular development. Using CLI makes work faster and easier.
Main uses:
- Creating a fresh Angular application
- Running the application
- Generating components, services, pipes, etc.
- Creating application build
- Running unit tests
Because of this, Angular CLI is a very important tool.
Installing Angular CLI
Angular CLI is installed using npm (Node Package Manager).
Important points:
- npm is required to install Angular CLI
- CLI is installed globally using
-g - After installation,
ngcommands can be used from any folder
Once installed, all Angular commands use ng, not npm.
Checking Angular CLI Installation
To check whether Angular CLI is installed or not, run this command:
ng version
This command shows:
- Angular version
- Angular CLI version
- Node version
- Package manager version
- Operating system details
If this command does not work, it means Angular CLI is not installed properly.
Angular CLI Help Command
Angular CLI provides a help command:
ng help
This command shows all available commands, such as:
- create new application
- build
- serve
- generate
- test
- update
- version
This is useful to understand what Angular CLI can do.
Creating a New Angular Application
To create a new Angular project, Angular CLI is used.
Main idea:
ng new app-name
This creates a fresh Angular application with proper folder structure.
Generating Components Using Angular CLI
Components are generated using Angular CLI commands.
Key points:
- Components are generated inside the
appfolder - The
appcomponent is the root component - All other components are created inside it
Commands:
- Full command:
ng generate component component-name - Short command:
ng g c component-name
Both commands do the same thing.
Generating Services
Services are used for things like API calls.
Important points:
- Services do not automatically create a folder
- It is better to create services inside a specific folder
Command example idea:
ng generate service service-name- Folder can be added using
/(folder/service-name)
Generating Pipes
Angular CLI can also generate pipes.
Key idea:
- Pipes can be created using
ng generate pipe pipe-name - Folder structure can also be used
- CLI generates required files automatically
Even if a typo happens, CLI still creates files based on the command.
Running the Angular Application
To run the application:
ng serve
To automatically open the browser:
ng serve --open
This runs the app and opens it in the browser at the same time.
Building the Application
Angular CLI is also used to create the build of the application.
ng build
This command generates a production-ready build.