Sub topic 3
Installing and Using the Angular CLI
The Angular CLI (command-line interface) is the official tool for creating, building, testing and maintaining Angular projects. Almost every task in this tutorial starts with an ng command, so this chapter covers installing it, how its commands are organized, how the global and project-local copies interact, and the errors that most often block a first install.
This chapter assumes you can use a terminal, have Node.js installed (an active or maintenance LTS release), and know what npm install does. Creating the project itself is covered in the next chapter, and serving and building are covered later.
What the CLI Does for You
An Angular app needs a TypeScript compiler, a template compiler, a bundler, a development server, a test runner and a set of configuration files that agree with each other. You could wire these together yourself. The CLI does it for you and keeps the setup consistent across projects and upgrades.
The CLI's work falls into four groups:
| Group | Typical commands | Purpose |
|---|---|---|
| Scaffolding | `ng new`, `ng generate` | Create workspaces and files from templates |
| Building and running | `ng serve`, `ng build` | Compile, bundle and serve the app |
| Quality | `ng test`, `ng lint`, `ng e2e` | Run tests and linters through configured builders |
| Maintenance | `ng add`, `ng update`, `ng config` | Install libraries, migrate versions, edit settings |
The scaffolding commands run schematics. A schematic is a code generator that writes or changes files according to rules. The build and test commands run builders, which are functions configured in angular.json that do a task such as compiling or testing. You rarely write either one yourself, but these names come up in error messages and documentation.
Installing the CLI
Check Node.js and npm first
Every Angular major version supports a specific range of Node.js versions. If your Node version is outside that range, installs can fail or the CLI can refuse to run. Check what you have:
node --version
npm --versionCompare the result with the version compatibility table in the Angular documentation (angular.dev, "Version compatibility"). An even-numbered LTS release that is currently active or in maintenance is the safe choice.
Install globally
The standard install puts the ng command on your PATH:
npm install -g @angular/cli
ng versionng version (alias ng v) prints the CLI version, the Node version, the OS and, inside a project, the versions of every Angular package. When you report a bug or ask for help, include this output.
The first time you run an ng command, the CLI may ask whether to share anonymous usage data with the Angular team. Either answer is fine. You can change it later with ng analytics enable or ng analytics disable.
Run without a global install
You can also run the CLI without installing it globally by using npx, which downloads the package temporarily and runs it:
npx @angular/cli@latest new my-appThis is handy on CI machines or locked-down laptops, and it makes sure you create the project with the latest CLI. After the project exists, you can run its local CLI with npx ng serve or through the npm scripts that ng new adds to package.json (npm start, npm test, npm run build).
Global CLI Versus Local CLI
This is the detail that confuses people most. Each Angular workspace lists @angular/cli as a dev dependency in its own package.json, so a project has its own local copy in node_modules. When you type ng inside a workspace, the global CLI looks for that local copy and hands the command to it.
As a result:
- A project always builds with the CLI version it was written for, even if your global CLI is newer. Upgrading the global CLI does not upgrade your projects.
- If you forget
npm installafter cloning a repo, there is no local CLI yet. You will see an error saying the local CLI could not be found or that the folder is not an Angular workspace. ng versionshows both versions. If the global version is newer than the local one, the CLI prints a warning. The warning is informational and does not mean anything is broken.
To upgrade a project, use ng update inside it (covered below), not npm install -g.
How CLI Commands Are Structured
Every command follows the same pattern:
ng <command> [arguments] [--options]Options use kebab-case (--skip-tests) and boolean options take --no- to turn them off (--no-standalone). Most commands have short aliases:
| Command | Alias | Example |
|---|---|---|
| `ng generate` | `ng g` | `ng g c header` |
| `ng serve` | `ng s` | `ng s --port 4300` |
| `ng build` | `ng b` | `ng b --configuration development` |
| `ng test` | `ng t` | `ng t --watch=false` |
| `ng version` | `ng v` | `ng v` |
Getting help
Built-in help lists every option for every command, so you don't need to memorize them:
ng help
ng generate --help
ng generate component --helpThe third command lists options specific to the component schematic, such as --inline-template, --inline-style, --skip-tests and --flat.
Generating code safely with --dry-run
ng generate creates files such as components, services, directives, pipes, guards and interfaces. Before you let it write anything, preview what it will do:
ng generate component user-card --dry-runThe output lists each file the schematic would create or update, followed by a note that no changes were made. Remove --dry-run (or its alias -d) to apply the changes. Common generator types you will use in this tutorial:
ng generate component header
ng generate service data
ng generate pipe truncate
ng generate guard auth
ng generate interface userPaths are relative to the project's source root, so ng g c features/profile creates src/app/features/profile/.
Adding and updating packages
ng add installs a library and then runs that library's setup schematic, which can edit config files for you:
ng add @angular/materialng update upgrades Angular packages and runs migrations, which are schematics that rewrite your code for API changes:
ng update
ng update @angular/core @angular/cliThe first command only reports which packages have updates available. The second performs the update. Start with a clean git working tree, update one major version at a time, and check the interactive update guide on angular.dev for manual steps.
Configuration and shell completion
ng config reads and writes values in angular.json, and --global targets your user-level CLI settings instead:
ng config cli.packageManager
ng config --global cli.packageManager pnpmng completion adds tab completion for commands and options to your shell (Bash or Zsh). Run it once and open a new terminal.
Common Installation Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ng: command not found` or `'ng' is not recognized` | npm's global bin folder is not on PATH | Run `npm prefix -g` and add its `bin` folder (or the folder itself on Windows) to PATH, or use `npx ng` |
| `EACCES: permission denied` during `npm install -g` | Global npm folder is owned by root | Use a Node version manager, or change npm's prefix to a folder you own. Avoid `sudo npm install -g` |
| `ng.ps1 cannot be loaded because running scripts is disabled` | PowerShell execution policy blocks scripts | Run `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned`, or use Command Prompt |
| `The Angular CLI requires a minimum Node.js version` | Node is too old or too new for this CLI | Switch to a supported Node version |
| `This command is not available when running the Angular CLI outside a workspace` | You ran `ng serve`, `ng build` or `ng generate` outside a project | `cd` into the folder that contains `angular.json` |
| Local CLI not found after cloning | Dependencies were not installed | Run `npm install` in the project folder |
A Quick Verification Run
Run this sequence to confirm your setup end to end. It creates a throwaway project without installing dependencies, then previews a generator inside it:
ng version
ng new cli-check --defaults --skip-install --skip-git
cd cli-check
npm install
ng generate component hello --dry-run
ng versionThe second ng version runs from inside the workspace and lists the project's Angular package versions as well as the CLI. If every step succeeds, your CLI is ready for the next chapter. You can delete cli-check afterward.
Exercise
Your global Angular CLI is at a newer major version than the @angular/cli listed in a project's package.json. You run ng build inside that project. Which CLI version does the build, and how should you upgrade the project to the newer version?
Show answer
The project's local CLI in node_modules does the build, because the global CLI delegates to it inside a workspace. To upgrade the project, run ng update @angular/core @angular/cli inside it, one major version at a time and from a clean git tree, so the migrations can update your code. Reinstalling the global CLI does not change the project.
Exercise
You want to see which files ng generate service api/user would create, and which options the service schematic accepts, without changing anything on disk. Which two commands do you run?
Show answer
Run ng generate service api/user --dry-run (or ng g s api/user -d) to preview the files, and ng generate service --help to list the schematic's options.
Exercise
A teammate on Windows installed the CLI successfully, but typing ng in PowerShell fails with "ng.ps1 cannot be loaded because running scripts is disabled on this system." What is happening, and name two ways to get unblocked.
Show answer
PowerShell's execution policy blocks the ng.ps1 wrapper script that npm created. Your teammate can allow locally created scripts for their user with Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned, or run ng from Command Prompt, which uses ng.cmd. Running npx ng from the project also works.