Angular 8 – CLI Commands

Angular CLI helps developers to create projects easily and quickly. As we know already, Angular CLI tool is used for development and built on top of Node.js, installed from NPM.This chapter explains about Angular 8 CLI commands in detail.

Verify CLI

Before moving to Angular CLI commands, we have to ensure that Angular CLI is installed on your machine. If it is installed, you can verify it by using the below command βˆ’

ng version

You could see the below response βˆ’

CLI

If CLI is not installed, then use the below command to install it.

npm install -g @angular/cli@^8.0.0

Let’s understand the commands one by one in brief.

New command

To create an application in Angular, use the below syntax βˆ’

ng new <project-name>

Example

If you want to create CustomerApp then, use the below code βˆ’

ng new CustomerApp

Generate Command

It is used to generate or modify files based on a schematic. Type the below command inside your angular project βˆ’

ng generate

Or, you can simply type generate as g. You can also use the below syntax βˆ’

ng g

It will list out the available schematics βˆ’

schematics

Let’s understand some of the repeatedly used ng generate schematics in next section.

Create a component

Components are building block of Angular. To create a component in angular use the below syntax βˆ’

ng g c <component-name>

For example, if user wants to create a Details component then use the below code βˆ’

ng g c Details

After using this command, you could see the below response βˆ’

CREATE src/app/details/details.component.scss (0 bytes)
CREATE src/app/details/details.component.html (22 bytes)
CREATE src/app/details/details.component.spec.ts (635 bytes)
CREATE src/app/details/details.component.ts (274 bytes)
UPDATE src/app/app.module.ts (1201 bytes)

Create a class

It is used to create a new class in Angular. It is defined belowβˆ’

ng g class <class-name>

If you want to create a customer class, then type the below command βˆ’

ng g class Customer

After using this command, you could see the below response βˆ’

CREATE src/app/customer.spec.ts (162 bytes)
CREATE src/app/customer.ts (26 bytes)

Create a pipe

Pipes are used for filtering the data. It is used to create a custom pipe in Angular. It is defined below βˆ’

ng g pipe <pipe-name>

If you want to create a custom digit counts in a pipe, then type the below command βˆ’

ng g pipe DigitCount

After using this command, you could see the below response βˆ’

CREATE src/app/digit-count.pipe.spec.ts (204 bytes)
CREATE src/app/digit-count.pipe.ts (213 bytes)
UPDATE src/app/app.module.ts (1274 bytes)

Create a directive

It is used to create a new directive in Angular. It is defined below βˆ’

ng g directive <directive-name>

If you want to create a UnderlineText directive, then type the below command βˆ’

ng g directive UnderlineText

After using this command, you could see the below response βˆ’

CREATE src/app/underline-text.directive.spec.ts (253 bytes)
CREATE src/app/underline-text.directive.ts (155 bytes)
UPDATE src/app/app.module.ts (1371 bytes)

Create a module

It is used to create a new module in Angular. It is defined below βˆ’

ng g module <module-name>

If you want to create a user information module, then type the below command βˆ’

ng g module Userinfo

After using this command, you could see the below response βˆ’

CREATE src/app/userinfo/userinfo.module.ts (194 bytes)

Create an interface

It is used to create an interface in Angular. It is given below βˆ’

ng g interface <interface-name>

If you want to create a customer class, then type the below command βˆ’

ng g interface CustomerData

After using this command, you could see the below response βˆ’

CREATE src/app/customer-data.ts (34 bytes)

Create a web worker

It is used to create a new web worker in Angular. It is stated below βˆ’

ng g webWorker <webWorker-name>

If you want to create a customer class, then type the below command βˆ’

ng g webWorker CustomerWebWorker

After using this command, you could see the below response βˆ’

CREATE tsconfig.worker.json (212 bytes)
CREATE src/app/customer-web-worker.worker.ts (157 bytes)
UPDATE tsconfig.app.json (296 bytes)
UPDATE angular.json (3863 bytes)

Create a service

It is used to create a service in Angular. It is given below βˆ’

ng g service <service-name>

If you want to create a customer class, then type the below command βˆ’

ng g service CustomerService

After using this command, you could see the below response βˆ’

CREATE src/app/customer-service.service.spec.ts (379 bytes)
CREATE src/app/customer-service.service.ts (144 bytes)

Create an enum

It is used to create an enum in Angular. It is given below βˆ’

ng g enum <enum-name>

If you want to create a customer class, then type the below command βˆ’

ng g enum CustomerRecords

After using this command, you could see the below response βˆ’

CREATE src/app/customer-records.enum.ts (32 bytes)

Add command

It is used to add support for an external library to your project. It is specified by the below command βˆ’

ng add [name]

Build command

It is used to compile or build your angular app. It is defined below βˆ’

ng build

After using this command, you could see the below response βˆ’

Generating ES5 bundles for differential loading...
ES5 bundle generation complete.

Config command

It is used to retrieve or set Angular configuration values in the angular.json file for the workspace. It is defined below βˆ’

ng config

After using this command, you could see the below response βˆ’

{
   "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
   "version": 1,
   "newProjectRoot": "projects",
   "projects": {
      "MyApp": {
         "projectType": "application",
         "schematics": {
            "@schematics/angular:component": {
               "style": "scss"
            }
         },
 .............................
 .............................

Doc command

It is used to open the official Angular documentation (angular.io) in a browser, and searches for a given keyword.

ng doc <keyword>

For example, if you search with component as ng g component then, it will open the documentation.

e2e command

It is used to build and serves an Angular app, then runs end-to-end tests using Protractor. It is stated below βˆ’

ng e2e <project> [options]

Help command

It lists out available commands and their short descriptions. It is stated below βˆ’

ng help

Serve command

It is used to build and serves your app, rebuilding on file changes. It is given below: βˆ’

ng serve

Test command

Runs unit tests in a project. It is mentioned below βˆ’

ng test

Update command

Updates your application and its dependencies. It is given below βˆ’

ng update

Version command

Shows Angular CLI version. It is stated below βˆ’

ng version

This Post Has One Comment

Leave a Reply