Sub topic 14
Adding libraries and updating with ng add and ng update
This chapter assumes you can run ng new, you know what package.json and angular.json contain, and you have seen what a schematic does when you run ng generate. Here you will learn how ng add and ng update differ from a plain npm install, and which errors to expect.
Why not just npm install?
npm install downloads a package into node_modules and records it in package.json. It does not touch your source code or angular.json. Many Angular libraries need more than that. They may need a provider in app.config.ts, a stylesheet in the build options, or a polyfill.
ng add closes that gap. It installs the package, then runs the package's ng-add schematic. A schematic is a script that edits your project files for you.
ng add @angular/materialThe command asks for a theme and whether to set up typography and animations. It then installs the package and edits several files. These typically include angular.json (theme stylesheet), src/index.html (fonts) and src/styles.css. Review the result with git diff before you commit.
The library must ship an ng-add schematic. If it does not, ng add installs the package and prints a warning that it cannot find the schematic. In that case, follow the library's manual setup steps.
Keeping dependencies current
ng update upgrades packages and applies migrations. A migration is a schematic that rewrites your code to match changes in the new version, such as a renamed API or a changed default.
ng updateWith no arguments, the command lists the packages that have updates. To upgrade, name them:
ng update @angular/core @angular/cliCommit your work first. ng update refuses to run when the Git working tree has uncommitted changes, unless you pass --allow-dirty. A clean tree lets you review the migration as a single diff.
Update one major version at a time, for example 18 to 19, then 19 to 20. Skipping versions is not supported, and the migrations are written for one step.
What happens during an update
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Repository is not clean` | Uncommitted changes | Commit or stash, or use `--allow-dirty` |
| `ERESOLVE` peer dependency conflict | A library does not yet support the new Angular version | Update that library first, or check its release notes |
| `ng add` finds no schematic | The package has no `ng-add` | Install it with npm and configure it by hand |
| Build fails after an update | A migration could not cover your code | Read the update guide at update.angular.dev and fix the flagged lines |
Exercise
You run ng update @angular/core @angular/cli and the command stops with "Repository is not clean". What should you do, and why is it better than the alternative?
Show answer
Commit or stash your changes, then run the command again. The migrations then appear as one separate diff that you can review or revert. --allow-dirty also works, but it mixes the migration edits with your own unfinished work.
Exercise
How does ng add differ from npm install for a library that provides an ng-add schematic?
Show answer
npm install only downloads the package and records it in package.json. ng add also runs the library's schematic, which can edit angular.json, app.config.ts, styles and other project files to set the library up.