Sub topic 1
What the Angular CLI does and how to install it
The Angular CLI is a command-line tool, run as the ng command, that creates, builds, tests and maintains Angular applications. This chapter covers what the CLI does, how it is installed, and how to confirm and fix the installation. The next chapter uses it to create a workspace.
Prerequisites
This chapter assumes you have:
- Basic TypeScript syntax and basic HTML and CSS. You won't write much of either yet.
- Node.js and npm installed. The CLI is a Node.js program and is distributed through npm.
- Comfort with a terminal such as Bash, zsh, PowerShell or Command Prompt.
Check Node.js and npm first:
node --version
npm --versionEach Angular major version supports a specific range of Node.js versions. The ranges are published in the version compatibility table in the Angular documentation. Use an active LTS (long-term support) release of Node.js. The CLI refuses to run on Node.js versions that the installed Angular release doesn't support.
What the CLI does
Angular applications need a compiler, a bundler, a development server, a test runner and a consistent file layout. The CLI wires these together behind one command, so you don't configure each tool yourself. Its work falls into four areas:
- Scaffolding.
ng newcreates a workspace, andng generateadds components, services and other artifacts that follow the project's conventions. - Running and building.
ng servestarts a development server with live reload.ng buildproduces optimized files you can deploy. - Testing and quality.
ng testruns unit tests. - Maintenance.
ng addinstalls a library and configures it.ng updateupgrades Angular packages and rewrites code that uses removed APIs.
The CLI does not replace npm. npm still downloads and stores packages. The CLI reads your project configuration and decides what to run.
Commands such as generate are carried out by schematics, which are code generators that edit your files. Commands such as build and serve are carried out by builders, which run a task using options from the workspace configuration. A later chapter covers both in detail. For now, note that the CLI is a thin dispatcher and the real work lives in packages.
Installing the CLI
The usual installation is global, so ng is available in every directory:
npm install -g @angular/cliConfirm that it worked:
ng versionThe output lists the CLI version, the Node.js version, your operating system and the versions of the installed @angular/* packages. Outside a workspace, the Angular package list shows as "Angular: Could not find Angular." That is expected. The packages appear once you run the command inside a project.
Running without a global install
If you can't or don't want to install globally, npx downloads and runs the CLI for a single command:
npx @angular/cli@latest new my-appYou can also pin a major version, for example npx @angular/cli@19 new my-app. This is useful when you must create a project with an older Angular version than your global CLI provides.
Global versus local CLI
A generated workspace also lists @angular/cli in its own devDependencies. When you run ng inside a workspace, the global command checks for a local copy and hands the work to it. So the version that executes ng build is the one your project pins, not the global one.
This has a practical consequence. A newer global CLI can safely work with an older project, and each project upgrades on its own schedule. Two projects on different Angular versions can sit side by side on one machine.
You can also call the local CLI directly, which avoids the global install entirely:
npx ng version| Approach | Command | Best for | Drawback |
|---|---|---|---|
| Global install | `npm install -g @angular/cli` | Daily development | Can lag behind the latest release |
| npx one-off | `npx @angular/cli@latest new app` | Trying the CLI, scripts, CI | Downloads the package on each use unless cached |
| Local only | `npx ng ...` inside a workspace | Locked, reproducible setups | Longer commands |
First-run behavior
The first time you run a command, the CLI may ask two questions:
- Analytics. It asks whether to share anonymous usage data with Google. You can answer either way, and you can change the answer later with
ng analytics disableorng analytics enable. - Autocompletion. It offers to add tab-completion for
ngcommands to your shell profile. This works in Bash, zsh and similar shells. You can set it up later withng completion.
Neither prompt affects how projects are built.
Getting help
The CLI documents itself, and its output always matches the installed version:
ng help
ng generate --help
ng generate component --helpThe first command lists all commands. The second lists the generator types, such as component, service and pipe. The third lists every option for one generator. When a flag from a tutorial doesn't work, check --help, because options change between releases.
The main commands you will meet in this tutorial are:
| Command | Purpose |
|---|---|
| `ng new` | Create a workspace and its first application |
| `ng generate` (alias `g`) | Generate code from a schematic |
| `ng serve` | Build and serve with live reload |
| `ng build` | Produce deployable output |
| `ng test` | Run unit tests |
| `ng add` | Add and configure a library |
| `ng update` | Upgrade packages and migrate code |
| `ng version` | Show tool and package versions |
Updating and removing the CLI
To update the global CLI, reinstall it at the latest version:
npm install -g @angular/cli@latestThis changes only the global tool. It does not upgrade existing projects. Use ng update inside a project for that.
To remove it:
npm uninstall -g @angular/cliCommon errors
ng: command not found, or "not recognized". The global npm bin directory isn't on yourPATH. Runnpm config get prefixand make sure itsbindirectory (on Windows, the prefix itself) is on thePATH. Then open a new terminal.EACCESpermission denied during install on macOS or Linux. Don't usesudo. Fix the cause by pointing npm's prefix at a directory you own, or install Node.js through a version manager such as nvm.- "Running scripts is disabled on this system" in Windows PowerShell. PowerShell's execution policy blocks the
ng.ps1launcher. A common fix is to runSet-ExecutionPolicy RemoteSigned -Scope CurrentUser. Alternatively, use Command Prompt. - "The current Node.js version is not supported." The installed Node.js version is outside the range the CLI supports. Switch to a supported LTS release.
- Unexpected CLI version inside a project. The local copy wins over the global one. Run
ng versioninside the project folder to see which version actually runs.
Exercises
Exercise
You have a global CLI at one major version, and you run ng build inside an older project that lists a different CLI version in its devDependencies. Which version runs the build, and how can you confirm it?
Show answer
The project's local CLI runs the build, because the global ng command delegates to the local copy when one exists in the workspace. Confirm by running ng version (or npx ng version) inside the project folder and reading the CLI version it reports.
Exercise
A teammate gets EACCES when running npm install -g @angular/cli on Linux and proposes prefixing the command with sudo. Give one reason this is a poor fix and one better alternative.
Show answer
Using sudo creates root-owned files in npm's directories, which causes more permission errors on later installs and updates. Better alternatives are installing Node.js through a version manager such as nvm, or configuring npm's prefix to a directory the user owns.