What is the Angular CLI?
The Angular CLI (Command Line Interface) is a powerful tool used to initialize, develop, scaffold, and maintain Angular applications directly from a command shell. As an Engineering Manager or Lead, the CLI is your best friend for enforcing consistency across your team—ensuring that every component, service, and module follows the official style guide automatically.
- Essential Development Commands
These commands are the daily drivers for any Angular developer. They manage the lifecycle of the application from creation to local execution.
- ng new <name>: Creates a new Angular workspace and an initial application.
- ng serve: Builds the application and starts a local development server (usually at
localhost:4200). It features 'Live Reload' to refresh the browser on save. - ng build: Compiles the application into an output directory (
dist/) for deployment. - ng test: Runs unit tests using Karma and Jasmine.
- The 'Generate' Blueprint
Instead of creating files manually, the ng generate (or ng g) command uses schematics to create files with the correct boilerplate and registration logic. This is critical for maintaining a clean architecture.
Angular CLI Lifecycle
The CLI handles the entire journey of a piece of code, from the moment you scaffold it to the moment it is optimized for production.
Common CLI Command Reference
| Command | Short Alias | Description |
|---|---|---|
| ng generate | ng g | Creates new files (components, pipes, etc.) |
| ng serve --open | ng s -o | Runs app and opens in default browser |
| ng build --configuration production | ng b | Builds optimized bundle for prod |
| ng lint | ng l | Runs linting tools on your code base |
| ng add | n/a | Installs a library and sets up configuration |
- The Power of 'ng add'
The ng add command is a game-changer for architects. It doesn't just install a package; it runs a script to automatically configure your project to use that library. For example, adding Angular Material handles all the theme imports and module registrations for you.