Report this

What is the reason for this report?

Cheatsheet: Angular CLI Reference

Updated on October 28, 2020
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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Alligator
Alligator
Author
See author profile

Alligator.io is a developer-focused resource that offers tutorials and insights on a wide range of modern front-end technologies, including Angular 2+, Vue.js, React, TypeScript, Ionic, and JavaScript.

Matt Abrams
Matt Abrams
Editor
See author profile

Supporting the open-source community one tutorial at a time. Former Technical Editor at DigitalOcean. Expertise in topics including Ubuntu 22.04, Ubuntu 20.04, CentOS, and more.

Category:
Tags:

Still looking for an answer?

Was this helpful?


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

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.