// Tutorial //

Cheatsheet: Angular CLI Reference

Published on April 11, 2017 · Updated on October 27, 2020
Default avatar
By Alligator.io
Developer and author at DigitalOcean.
Cheatsheet: Angular CLI Reference

Introduction

Here’s a cheat sheet that will help you find the commands you need for most of the things that you would want to do with the Angular CLI. For a brief introduction to the Angular CLI you can explore this tutorial.

Checking the Version

See which version of the CLI you’re using:

  1. ng --version

Updating the Version

Run this:

  1. npm uninstall -g @angular/cli cache clean
  2. npm install -g @angular/cli@latest

Help

Get general help:

  1. ng help

Or get help for a specific command:

  1. ng help generate

New Project

Generate a new project:

  1. ng new my-app

And here are a few flags you can use:

  • --dry-run: See which files would be created, but don’t actually do anything.
  • --verbose: Be more chatty.
  • --skip-install: Don’t npm install, useful when offline or with slow internet.
  • --skip-tests: Don’t create spec files.
  • --skip-git: Don’t initialize a git repo.
  • --source-dir: Name of the source directory
  • --routing: Add routing to the app.
  • --prefix: Specify the prefix to use for components selectors.
  • --style: Defaults to css, but can be set to scss.
  • --inline-style: Use inline styles for components instead of separate files.
  • --inline-template: Use inline templates for components instead of separate files.

Here’s an example with a few flags:

  1. ng new my-app --prefix yo --style scss --skip-tests --verbose

Generate All The Things

Generate a component:

  1. ng g c unicorn-component

Generate a service:

  1. ng g s everything-service

Generate a pipe:

  1. ng g pipe my-pipe

Generate a directive:

  1. ng g directive my-directive

Generate an enum:

  1. ng g enum some-enum

Generate a module:

  1. ng g module fancy-module

Generate a class:

  1. ng g cl my-class

Generate an interface:

  1. ng g interface my-interface

Generate a route guard:

  1. ng g guard my-guard

The --dry-run and --verbose flags can be used with any generate command.

Serving

Serve your project

  1. ng s

Serve and open in your default browser automatically:

  1. ng s -o

Serve to a different port:

  1. ng s --port 5555

Running Your Tests

  1. ng test

And some flags you can use with the test command:

  • --watch: Retest when some files change.
  • --code-coverage: Add a coverage report.
  • --progress: Show the progress while running the tests.
  • --browsers: Specify which browsers to use.
  • --colors: Use colors in the output or not.

Linting

Run the linter:

  1. ng lint

A few flags for the linter:

  • --fix: Apply fixes for linting errors.
  • --force: Force success even when linting fails.

Building Your App

Build your app with the build command:

  1. ng build

And here are some flags that you can use with build:

  • --target: Specify the target for the build (e.g.: --target production).
  • --aot: Use ahead of time compilation.
  • --base-href: Specify the base href to use.
  • --deploy-url: Specify the deployment url.
  • --extract-css: Put the global styles in a CSS file instead of keeping it in the JavaScript.
  • --watch: Rebuild every time a file changes.

Ejecting Your Webpack Config

The Angular CLI doesn’t do it for your project anymore? Just eject, and you’ll have the full Webpack config available to tweak to your heart’s desire:

  1. ng eject

Conclusion

These are some of the most common commands for the Angular CLI. For a brief introduction to the Angular CLI, you can explore this tutorial.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
1 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thank you