Building and Serving Angular Apps with CLI
Introduction
The Angular Command Line Interface (CLI) is a powerful tool that simplifies the development of Angular applications. It provides commands to create, build, serve, and test Angular apps, enhancing productivity and standardization across projects.
Angular CLI Installation
To use Angular CLI, you need to have Node.js installed. Once Node.js is installed, you can install Angular CLI globally via npm:
npm install -g @angular/cli
Verify the installation by checking the version:
ng version
Creating an Angular App
Creating a new Angular application is straightforward with the CLI. Use the following command:
ng new my-angular-app
Follow the prompts to select options such as routing and stylesheet format.
Building an Angular App
To build your Angular application for production, run:
ng build --prod
This command generates a dist
folder containing the compiled files optimized for deployment.
Serving an Angular App
To serve your application locally, use:
ng serve
By default, your app will be accessible at http://localhost:4200
. You can specify a different port with:
ng serve --port 4300
Best Practices
- Keep your CLI updated:
npm update -g @angular/cli
- Use environment configurations for different stages (development, production).
- Regularly build and test your application to catch errors early.
- Utilize Angular's built-in tools for optimization during the build process.
FAQ
What is Angular CLI?
Angular CLI is a command line interface tool to automate the development workflow of Angular applications.
Can I serve my app on a different port?
Yes, you can specify a different port using the --port
flag with the ng serve
command.
How can I build a production-ready version of my app?
Run ng build --prod
to create a production build of your Angular application.