Video lecture
Watch on YouTube(opens in a new tab)Sub topic 3
Installing and Using the Angular CLI
This chapter assumes you have Node.js installed (an active or maintenance LTS release), know how to run npm commands in a terminal, and understand what a global npm package is.
What the CLI Does
The Angular CLI is a command-line tool named ng. It creates workspaces, generates code, runs the development server, builds bundles and runs tests. Angular projects come with a full build pipeline already set up, and the CLI hides that setup from you. You don't write bundler config by hand. You run ng commands and the CLI reads angular.json to decide what to do.
Installing It Globally
Install the CLI once per machine, then check that it works:
npm install -g @angular/cli
ng versionng version prints the CLI version, your Node.js version, your package manager and your operating system. If you run it inside a project, it also lists the Angular packages that project uses. Run it first whenever something behaves oddly, because many problems come from version mismatches.
Global Versus Local CLI
Every generated workspace also installs @angular/cli locally in node_modules. When you run ng inside a workspace, the global binary hands the command to the local copy. So the project's own version decides how its commands behave, even if your global version is newer.
The Commands You Will Use
| Command | Alias | Purpose |
|---|---|---|
| `ng new <name>` | `ng n` | Create a new workspace and app |
| `ng serve` | `ng s` | Run the dev server with live reload |
| `ng generate <type> <name>` | `ng g` | Create components, services, guards and more |
| `ng build` | `ng b` | Produce an optimized build in `dist/` |
| `ng test` | `ng t` | Run unit tests |
| `ng add <package>` | Install a library and run its setup schematic | |
| `ng update` | Upgrade Angular packages and migrate your code |
Later chapters cover ng new, ng serve and ng test in detail. Use --help on any command to see its options:
ng help
ng generate component --help
ng generate component header --dry-run--dry-run lists the files a command would create or change without writing anything. Use it before running an unfamiliar generator.
Common Errors
ng: command not found: npm's global bin folder is not on yourPATH. Runnpm prefix -gto find the folder, then add itsbinsubfolder (on Windows, the folder itself) toPATH.EACCESpermission errors on macOS or Linux: don't usesudo. Install Node.js through a version manager such as nvm so global packages go into your home directory.- Windows PowerShell blocks
ng.ps1: allow local scripts withSet-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned. - "Unsupported Node.js version" warning: each Angular major version supports only certain Node.js releases. Switch to a supported LTS release.
Exercise
Your global CLI is version 20, but ng version inside an older project reports CLI 18. Which version runs ng generate component in that project, and why?
Show answer
Version 18. Inside a workspace, the global ng binary hands the command to the local @angular/cli in node_modules, so the project's own CLI version applies.
Exercise
How can you preview the files that ng generate service data would create without writing them?
Show answer
Add the dry-run flag: ng generate service data --dry-run.