Sub topic 14
Adding libraries and updating with ng add and ng update
This chapter assumes you can run ng commands, read package.json, and know what a semantic version range such as ^19.0.0 means. You should also have a workspace created with ng new, as covered earlier in this tutorial.
Why the CLI has its own package commands
npm install puts a package into node_modules and records it in package.json. That is all it does. Many Angular libraries also need changes to your project: a provider in app.config.ts, an entry in angular.json, a stylesheet, or a polyfill. Doing those edits by hand is error-prone.
The CLI adds two commands that combine package management with code changes:
ng addinstalls a package and then runs that package's setup schematic.ng updateupgrades packages and then runs migration schematics that rewrite your code for the new version.
A schematic is a script shipped inside a package that reads and changes files in your workspace. Both commands rely on schematics, which a later chapter covers in more detail.
Adding a library with ng add
The general form is:
ng add @angular/materialThe CLI downloads the package, asks you any questions the schematic defines, and applies the changes. For Angular Material, the prompts cover a theme and whether to set up global typography and animations. The schematic then edits package.json, angular.json (to register the theme stylesheet), and src/index.html (to add fonts).
You can pass a version and skip prompts:
ng add @angular/material@19 --skip-confirmationOther first-party and common packages work the same way. For example, ng add @angular/pwa adds a service worker and a web manifest, and ng add @angular/ssr adds server-side rendering to an existing project.
What if the package has no ng-add schematic?
ng add only works for packages that declare an ng-add schematic. If the package does not, the CLI reports an error. The package itself is still installed, so use npm install for ordinary libraries such as date-fns or rxjs extras, and then import them in your code.
| Situation | Command |
|---|---|
| Library needs project configuration (Material, PWA, SSR) | `ng add <package>` |
| Plain utility library with no setup | `npm install <package>` |
| Tooling used only while developing | `npm install --save-dev <package>` |
Working in a multi-project workspace
If the workspace has several projects, pass --project so the schematic edits the right one:
ng add @angular/material --project my-appUpdating with ng update
Run ng update with no arguments to see what can be updated:
ng updateThe output lists Angular packages and other packages that provide update metadata, each with the installed and recommended versions. To update, name the packages:
ng update @angular/core @angular/cliAlways update @angular/core and @angular/cli together. They are released in step, and the CLI performs the migrations for both. Other Angular packages such as @angular/material should follow in a second command, after the first one succeeds.
What happens during an update
- The CLI reads the update metadata (the
ng-updatefield in each package'spackage.json). - It installs the new versions and updates
package.json. - It runs migration schematics, in order, for every version between your old and new version.
- It leaves the changes in your working tree for you to review.
For example, a migration might rename a deprecated API, move a configuration option, or convert syntax. The CLI prints a summary of what it migrated.
Update one major version at a time
Angular supports upgrading one major version per step. To go from 17 to 19, update to 18 first, then to 19:
ng update @angular/core@18 @angular/cli@18
ng update @angular/core@19 @angular/cli@19Skipping a major version can miss migrations that the intermediate release provides. The official Angular Update Guide at angular.dev/update-guide lists version-specific steps for each hop.
Useful options
| Option | Effect |
|---|---|
| `--next` | Update to a pre-release version |
| `--force` | Proceed even when peer dependency checks fail |
| `--allow-dirty` | Run even with uncommitted changes |
| `--create-commits` | Create one Git commit per migration |
| `--migrate-only` | Run migrations only, without installing new versions |
| `--from` and `--to` | Limit the migration range, used with `--migrate-only` |
--create-commits is helpful because each migration becomes a separate commit that you can inspect or revert on its own.
ng update @angular/core@19 @angular/cli@19 --create-commitsCommon errors and how to read them
Uncommitted changes. By default ng update refuses to run if Git shows a dirty working tree. This protects you: migrations edit many files, and a clean tree lets you see exactly what changed. Commit or stash first rather than reaching for --allow-dirty.
Peer dependency conflicts. A third-party package may declare that it supports only older Angular versions. The error mentions ERESOLVE or an incompatible peer dependency. The preferred fix is to update that package to a version that supports your target Angular version. Use --force only when you have checked that the package really works, because it hides a genuine incompatibility.
Package not found in update metadata. Packages without an ng-update field cannot run migrations. Update them with npm install package@version and check their changelog yourself.
Mismatched Angular versions. Mixing versions, for example @angular/core at 19 and @angular/router at 18, causes confusing build errors. After an update, check that all @angular/* entries in package.json share the same major version.
Checking the result
After either command, confirm that the project still works:
git diff --stat
ng build
ng test --watch=falsegit diff --stat shows which files were touched. A successful build and test run confirm that the migrations left the project in a working state. If something fails, you can compare against your pre-update commit and revert specific files.
Summary of the workflow
- Commit your current work.
- Run
ng updateto see what is outdated. - Update
@angular/coreand@angular/clitogether, one major version at a time. - Update other Angular packages, then third-party packages.
- Review the diff, build, and run tests.
- Use
ng addfor any new library that provides a setup schematic, andnpm installfor the rest.
Exercise
You are on Angular 17 and want to reach Angular 19. You also use @angular/material. Describe the sequence of commands you would run, and explain why you would not run a single command that jumps straight to 19.
Show answer
Commit a clean working tree. Run ng update @angular/core@18 @angular/cli@18, then ng update @angular/material@18, then build and test. Repeat with version 19: ng update @angular/core@19 @angular/cli@19, then ng update @angular/material@19. Update one major version at a time because each major release ships its own migration schematics, and skipping a version can skip migrations that your code needs.
Exercise
You run ng add some-utility-lib and the CLI reports that the package does not support ng add. What has happened to the package, and what should you do next?
Show answer
The package is installed in node_modules and listed in package.json, but no setup schematic ran because the package does not define an ng-add schematic. Use it like any npm package: import it in your code and add any configuration it requires by hand, following its documentation.
Exercise
Why is --create-commits useful with ng update, and what must be true before you run the command?
Show answer
It records each migration schematic as its own Git commit, so you can inspect or revert a single migration. Git must be available and the working tree must be clean (or you must pass --allow-dirty), because the CLI needs to commit only its own changes.