Site icon Tutorial

Angular CLI

Go back to Tutorial

The Angular CLI is a tool to initialize, develop, scaffold and maintain Angular applications

Installing Angular 7 CLI

To install the Angular CLI, execute the command:

npm install -g @angular/cli

If you get an error installing the CLI, this is an issue with your local npm setup on your machine, not a problem in Angular CLI.

Verifying Installation – Generating and serving an Angular project via a development server Create and run a new project:

ng new my-project

cd my-project

ng serve

Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Installing a specific version – The CLI is installed both globally (the command above with the -g argument to npm install) and also within the project. To install a different version of the CLI, you can just update the version locally within your project. The globally installed package will always delegate to the local one.

There are several different versions available at any time:

Angular 7 CLI Commands

ng serve

ng serve builds the application and starts a web server.

ng serve [project]

Options

 

ng generate

ng generate [name] generates the specified schematic

Alias

g – ng g [name]

Available Schematics:

Options

 

ng lint

ng lint will lint you app code using tslint. The syntax is: ng lint [project]

Options

Output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist, codeFrame).

ng test

ng test compiles the application into an output directory

Run unit tests by command

ng test

Tests will execute after a build is executed via Karma, and it will automatically watch your files for changes. You can run tests a single time via –watch=false.

You can run tests with coverage via –code-coverage. The coverage report will be in the coverage/ directory.

Options

 

ng e2e

ng e2e serves the application and runs end-to-end tests.

ng e2e [project]

TO run end-to-end tests, use command

ng e2e

End-to-end tests are run via Protractor.

Options

 

ng build

ng build compiles the application into an output directory.

ng build [project]

Creating a build

ng build

The build artifacts will be stored in the dist/ directory.

All commands that build or serve your project, ng build/serve/e2e, will delete the output directory (dist/ by default). This can be disabled via the –delete-output-path=false option.

Base tag handling in index.html – When building you can modify base tag (<base href=”/”>) in your index.html with –base-href your-url option.

# Sets base tag href to /myUrl/ in your index.html

ng build –base-href /myUrl/

Bundling & Tree-Shaking – All builds make use of bundling and limited tree-shaking, while –prod builds also run limited dead code elimination via UglifyJS.

–build-optimizer and –vendor-chunk

When using Build Optimizer the vendor chunk will be disabled by default. You can override this with –vendor-chunk=true.

Total bundle sizes with Build Optimizer are smaller if there is no separate vendor chunk because having vendor code in the same chunk as app code makes it possible for Uglify to remove more unused code.

CSS resources – Resources in CSS, such as images and fonts, will be copied over automatically as part of a build. If a resource is less than 10kb it will also be inlined. You’ll see these resources be outputted and fingerprinted at the root of dist/.

ES2015 support – To build in ES2015 mode, edit ./tsconfig.json to use “target”: “es2015” (instead of es5). This will cause application TypeScript and Uglify be output as ES2015, and third party libraries to be loaded through the es2015 entry in package.json if available.

Options

ng config

ng config [key] [value] Get/set configuration values. [key] should be in JSON path format. Example: a[3].foo.bar[2]. If only the [key] is provided it will get the value. If both the [key] and [value] are provided it will set the value.

Options

global – Get/set the value in the global configuration (in your home directory). The syntax is: –global (alias: -g)

ng doc

ng doc [search term] Opens the official Angular API documentation for a given keyword on angular.io.

Options

 

ng update

ng update Updates the current application to latest versions.

ng update [package]

Options

 

ng new

ng new [name] creates a new angular application.

Default applications are created in a directory of the same name, with an initialized Angular application.

Options

 

ng xi18n

ng xi18n Extracts i18n messages from the templates.

ng xi18n [project]

Options

Angular CLI workspace file (angular.json) schema

Properties

 CLI Prompts

The CLI will now prompt users when running common commands like ng new or ng add @angular/material to help you discover built-in features like routing or SCSS support.

CLI Prompts have been added to Schematics, so any package publishing Schematics can take advantage of them by adding an x-prompt key to a Schematics collection.

“routing”: {

“type”: “boolean”,

“description”: “Generates a routing module.”,

“default”: false,

“x-prompt”: “Would you like to add Angular routing?”

}

Go back to Tutorial

Exit mobile version