Angular CLI: Tips and Tricks for Productive Development


5 min read

Angular CLI (Command Line Interface) is a powerful tool that simplifies and accelerates Angular development. Whether you're a beginner or an experienced developer, there are numerous tips and tricks you can use to streamline your workflow and become more productive when working with Angular CLI.

In this blog, we'll explore a range of Angular CLI tips and tricks, organized into subsections for ease of navigation.

1. Installation and Setup

Installing Angular CLI

To start using Angular CLI, you need to install it globally on your machine. Open your terminal or command prompt and run the following command:

npm install -g @angular/cli

This command installs Angular CLI globally, making it accessible from any directory.

Checking the Installed Version

To check the installed Angular CLI version, use the command:

ng --version

Updating Angular CLI

Regular updates are essential to keep your development environment up to date. To update Angular CLI to the latest version, run:

ng update @angular/cli

Generating a New Angular Project

Creating a new Angular project is effortless with Angular CLI. To generate a new project, use the ng new command:

ng new my-angular-app

This command prompts you to configure various project options, such as routing and stylesheets. Once configured, Angular CLI sets up a new project with all the necessary files and dependencies.

2. Generating Components, Modules, and Services

Generating Components

Components are the building blocks of Angular applications. You can quickly generate a new component using Angular CLI:

ng generate component my-component

This command creates a new component, including the component class, template, and styles, and registers it in the appropriate module.

Generating Modules

Modules help organize your application by grouping related components and services. Create a new module with the following command:

ng generate module my-module

Angular CLI generates a module file and adds it to the appropriate module's imports array.

Generating Services

Services are used for sharing data and functionality across components. To generate a new service, use the following command:

ng generate service my-service

This command generates a service class and registers it with the Angular dependency injection system.

Creating Routes with ng generate

Angular CLI can also help you set up routing in your application. To generate a new route, use:

ng generate module app-routing --flat --module=app

This command generates a routing module and configures it in the main app module.

3. Configuration and Customization

Angular CLI Configuration

Angular CLI provides a configuration file, angular.json, where you can customize various aspects of your project, including build and serve options, assets, and more. You can manually edit this file or use Angular CLI commands to make changes.

For example, to change the output directory for builds, use:

ng config projects.<project-name> dist/my-app

Customizing Angular Build and Serve Options

When serving your Angular application, you can customize the host and port using the --host and --port flags:

ng serve --host --port 4200

Environment Configuration

Angular CLI allows you to define environment-specific configuration files. By default, there are two environment files: environment.ts and You can add custom environment files and use them as needed.

To create a new environment file, run:

ng generate application <app-name> --configurations=my-config

This command generates a new configuration for your app, which you can then use to define a custom environment file.

4. Project Maintenance

Linting and Code Formatting

Linting and code formatting help maintain code quality and consistency. Angular CLI includes linting configurations for tools like TSLint and ESLint. You can run linting checks with:

ng lint

To automatically fix linting issues, use:

ng lint --fix

Running Unit Tests

Angular CLI uses Jasmine and Karma for unit testing. To run your unit tests, execute:

ng test

This command launches a test runner and provides test results, including code coverage.

Building for Production

To build your Angular application for production, use:

ng build --prod

This command optimizes your application for production, including minification and tree shaking, resulting in smaller bundle sizes.

Cleaning Up Unused Code

Over time, your project may accumulate unused files and dependencies. Angular CLI provides a convenient command to clean up your project:

ng prune

This command removes unused packages and files from your project, helping you keep it clean and efficient.

5. Advanced Techniques

Generating a Library

Angular CLI supports generating libraries. To create a reusable library, use:

ng generate library my-library

This command generates a library project that you can build, test, and distribute independently.

Running Multiple Apps in a Single Project

Angular CLI allows you to manage multiple applications within a single project. To create a new app in an existing project, use:

ng generate application my-other-app

You can then serve, build, and test each app independently.

Using Third-Party Schematics

Many third-party libraries provide schematics to simplify integration with Angular CLI. For example, to add Angular Material to your project, you can use the following:

ng add @angular/material

This command installs Angular Material and configures it in your project, saving you time and effort.

These Angular CLI tips and tricks should help you become more proficient in Angular development. By leveraging the power of the command-line interface, you can streamline your workflow, automate repetitive tasks, and focus on building high-quality Angular applications.

Remember to explore the Angular CLI documentation for additional features and options to further enhance your development experience. Happy coding!

If you've enjoyed this guide and want to express your appreciation, please consider buying me a coffee. It fuels not only my caffeine addiction but also the creation of more valuable content for you.

Buy Me a Coffee

Thank you for your support! ๐Ÿ˜

Did you find this article valuable?

Support Ayush Agarwal by becoming a sponsor. Any amount is appreciated!