Keep an Angular Project Up to Date: A Practical Guide
In this article
Keeping an Angular project up to date ensures you benefit from the latest performance improvements, bug fixes, and security patches. This guide walks you through the process of updating both the Angular CLI and your project's dependencies, from checking your current version to running the update schematics and handling any breaking changes.
What You'll Need
- An existing Angular project (Angular 2 or later)
- Node.js and npm installed (check with
node -vandnpm -v) - Access to a terminal or command prompt
- Basic familiarity with the command line
Step-by-Step
-
Check your current Angular version
Runng versionin your project root. This shows the Angular CLI version and the versions of the Angular packages installed. Note the major version (e.g., 15, 16, 17) – you'll use this to plan your update path. -
Back up your project
Before making any changes, commit all your work to version control (git) or create a zip copy of the project folder. This ensures you can revert if something goes wrong. -
Update the Angular CLI globally (optional but recommended)
Runnpm install -g @angular/cli@latestto get the latest CLI. If you prefer to keep the global version, you can skip this step and usenpxto run the latest CLI commands. -
Run the Angular update command
In your project directory, runng update @angular/core@latest @angular/cli@latest. This will check for updates and apply the necessary changes to your package.json and source files. If you're updating from an older major version (e.g., from 14 to 15), you may need to update step-by-step (e.g., 14→15, then 15→16). The command will warn you if that's the case. -
Review and apply the update schematics
Theng updatecommand may prompt you to run schematics that modify your code (e.g., to rename APIs or adjust configuration). Read the prompts carefully and answer accordingly. If you're unsure, choose the default option (usually "y"). -
Update other dependencies
After updating Angular core and CLI, runng update(without arguments) to see a list of other packages that have updates. You can update them individually, e.g.,ng update @angular/material, or runng update --allto update everything at once. Be cautious with major updates to libraries like RxJS or TypeScript – they may require additional code changes. -
Update TypeScript and other tooling
Angular often requires a specific TypeScript version. The update command usually handles this, but you can check your package.json and adjust if needed. For example, Angular 17 requires TypeScript 5.2 or later. -
Run your tests and build
After the update, runng testto run unit tests andng buildto ensure the project compiles. Fix any errors that arise – they may be due to breaking changes in the new version. -
Check for deprecated features
The Angular compiler and CLI will often warn you about deprecated APIs. Address these warnings to keep your code clean and future-proof. -
Commit the changes
Once everything works, commit the updated package.json, package-lock.json, and any modified source files with a message like "chore: update Angular to v17".
Common Mistakes
- Skipping major versions: Updating from Angular 12 directly to 16 can cause many breaking changes at once. Always go step-by-step through each major version.
- Ignoring the update schematics: The schematics often make necessary code changes. Skipping them can lead to errors.
- Updating only the CLI: Updating the global CLI but not the project's dependencies can cause version mismatches. Always run
ng updatein the project. - Not checking peer dependencies: Sometimes npm installs with warnings about peer dependencies. Don't ignore these – they can cause runtime issues.
- Forgetting to update the lock file: Always commit the updated package-lock.json to ensure consistent installs across environments.
Tips
- Read the official Angular update guide at
update.angular.io– it provides a tailored checklist for your current and target versions. - Use a feature branch for the update so you can easily revert if needed.
- Run
ng update --nextif you want to try the next release candidate (not recommended for production). - Keep your project's dependencies as up-to-date as possible, but don't update everything at once – focus on Angular core first.
- If you use Angular Material, update it together with the core – they are designed to work together.
- After updating, run a quick smoke test of your app's main features to catch any runtime issues.
Troubleshooting
- Update fails with "The package.json is not valid": Check for syntax errors in your package.json or conflicting versions. Try removing node_modules and package-lock.json, then run
npm installagain. - Build errors after update: Look at the error messages – they often point to deprecated APIs or changed imports. Use the official Angular update guide to find the relevant migration steps.
- Tests fail with undefined errors: This can happen if a library you use hasn't been updated. Try updating that library to a version compatible with your new Angular version.
- CLI commands not recognized: If
ngis not found, ensure the CLI is installed globally or usenpx ngto run the local version. - Peer dependency warnings: Run
npm install --legacy-peer-depsif you have conflicting peer dependencies, but first try to resolve them by updating the relevant packages.
Frequently Asked Questions
How often should I update my Angular project?
It's best to update to each new major version within a few months of its release. This avoids accumulating too many breaking changes and ensures you have the latest security patches. Minor and patch updates can be done more frequently, even monthly.
Can I skip a major version?
Technically yes, but it's not recommended. Skipping versions means you'll have to handle all the breaking changes from multiple versions at once, which is error-prone. The Angular update guide will warn you if a direct update is not supported.
What is the difference between ng update and npm update?
ng update is Angular's own update tool. It not only updates package versions but also runs schematics to modify your code for breaking changes. npm update only updates package versions, which may leave your code incompatible.
Do I need to update Node.js when updating Angular?
Yes, each Angular version requires a minimum Node.js version. Check the Angular version compatibility table on the official docs. For example, Angular 17 requires Node.js 18.13 or later.
What if my project uses a lot of third-party libraries?
Many libraries are updated regularly to support new Angular versions. Check the library's changelog or GitHub issues for compatibility. If a library is not updated, you may need to find an alternative or wait before updating Angular.
Can I revert an update if something goes wrong?
Yes, if you committed your code before the update, you can revert to that commit. If you didn't, you can restore from your backup. It's always a good practice to have a backup before starting an update.