Upgrading an Angular application to a new major version can feel daunting, but the Angular team consistently strives to make the process as smooth as possible with robust ng update schematics. Angular v21 introduces significant internal changes (like zoneless change detection and Vitest), but many are handled automatically or are opt-in.
This chapter provides a comprehensive checklist and best practices for migrating your existing Angular application to v21.
Step 1: Prepare for Migration
Before you even touch the ng update command, it’s crucial to prepare your project:
- Backup Your Project: Always create a backup of your project before performing any major upgrade. You can commit your current state to Git or make a direct copy.
git commit -am "Pre-Angular v21 Update Snapshot" # Or simply: git checkout -b pre-angular-v21-update - Ensure Current Version Compatibility: Make sure your application is on a compatible version to upgrade to v21. Generally, you should upgrade one major version at a time (e.g., v19 -> v20 -> v21). If you’re on a much older version, follow the official Angular Update Guide for incremental upgrades.(Check if you’re on v20.x.x. If not, upgrade to v20 first).
ng version - Update Node.js & npm/yarn: Ensure you’re using Node.js and npm/yarn versions compatible with Angular v21. Check the official Angular documentation for current requirements.
node -v npm -v - Clean
node_modules: Removenode_modulesandpackage-lock.json(oryarn.lock) to ensure a fresh install of dependencies.rm -rf node_modules package-lock.json npm install # Or with yarn: rm -rf node_modules yarn.lock && yarn install - Run All Tests: Ensure all your existing unit and end-to-end tests pass before the upgrade. This provides a baseline to quickly identify issues introduced by the update.
ng test ng e2e # if you have e2e tests configured - Check for Deprecated APIs: Review your code for any deprecated APIs from previous versions that might be removed in v21. The Angular CLI usually provides warnings for these during development.
Step 2: Update Angular CLI Globally (Optional but Recommended)
It’s often helpful to update your global Angular CLI to the latest major version before updating the project:
npm uninstall -g @angular/cli
npm install -g @angular/cli@next # Installs the latest v21 CLI
Step 3: Run the Angular Update Command
This is the core of the migration. The ng update command will analyze your project, update Angular packages, and run schematics to apply common code migrations.
cd your-project-folder
ng update @angular/core@21 @angular/cli@21
What happens during ng update:
- Package Updates:
@angular/core,@angular/cli, and other official Angular packages (@angular/common,@angular/router,@angular/forms, etc.) are updated to v21. - Automated Migrations: The CLI runs scripts to automatically:
- Adjust
app.config.ts(orAppModule) for new features. - If you had Zone.js, it might add
provideZoneChangeDetection()to keep your app working. (Your goal is to remove this later!). - Update RxJS imports/operators if necessary.
- Apply control flow migrations (
*ngIfto@if) if you hadn’t done it manually in v17+. - Potentially migrate some
CommonModuleimports to standalone. - (New in v21) Optionally migrate
NgClassto class bindings andNgStyleto style bindings.
- Adjust
Step 4: Address Post-Update Tasks and Manual Review
After ng update completes, there are typically a few things to check and potentially do manually:
- Review
ng updateOutput: The CLI provides valuable output, highlighting any potential issues, warnings, or manual steps required. Read this carefully. - Install Third-Party Updates: Update any third-party Angular libraries (e.g., Angular Material, NgRx, PrimeNG, etc.) to their v21 compatible versions. Check their official documentation for compatibility and migration guides.
ng update @angular/material@21 @angular/cdk@21 # Repeat for other @angular/* libraries and major third-party libraries npm install # Or yarn install after updating package.json dependencies - Run
ng serveandng build --configuration production:- Start your development server (
ng serve) and manually test your application. Check the browser console for any runtime errors or warnings. - Perform a production build to check for build errors and bundle size changes.
- Start your development server (
- Run Tests Again: Rerun all your unit and e2e tests.
ng test ng e2e- Vitest Transition: If you were on Karma/Jasmine,
ng updatetypically won’t automatically switch you to Vitest. Your tests should still run. If you want to switch to Vitest, this will be a separate, manual migration effort (as discussed in Chapter 7). - Zone.js Removal: If
ng updateaddedprovideZoneChangeDetection(), start gradually removing Zone.js dependencies (frompolyfills.tsandangular.json) and theprovideZoneChangeDetection()call itself. Test each step. This is where you fully embrace zoneless. fixture.detectChanges()in Tests: In zoneless tests, replace explicitfixture.detectChanges()after async operations withawait fixture.whenStable()if you were relying on Zone.js for auto-detection.
- Vitest Transition: If you were on Karma/Jasmine,
- Review Breaking Changes: Consult the official Angular CHANGELOG and breaking changes documentation for v21. Be aware of any changes that
ng updatecouldn’t handle automatically. - Adopt New Features Gradually:
- Safe to Adopt Immediately: HttpClient by default (no code changes needed if already using it), enhanced template syntax (direct bindings), build optimizations (automatic).
- Test Carefully: Zoneless change detection (after removing
provideZoneChangeDetection()), AI tools (experiment in non-critical areas). - Wait for Stable: Signal Forms (still experimental).
- Review Optional Migrations (v21 specific):Review the changes these migrations make carefully.
# Run these if you want to apply recommended style migrations ng generate @angular/core:ngclass-to-class-migration ng generate @angular/core:ngstyle-to-style-migration ng generate @angular/core:common-to-standalone # For breaking up CommonModule imports
Common Migration Gotchas & Troubleshooting
- Third-Party Library Incompatibility: The most common issue. Always update them and check their release notes. If an important library isn’t updated, you might need to wait or find an alternative.
- Zone.js-Dependent Libraries: Some older libraries (e.g., certain animation libraries, datepickers) might implicitly rely on Zone.js. If you go fully zoneless and encounter issues, you might need to use
markForCheck()or, as a last resort, temporarily keepprovideZoneChangeDetection(). - Polyfills: Ensure your
polyfills.tsfile still includes necessary polyfills for your target browsers, especially if you removedzone.js. - Strict Mode Issues: If you’re enabling stricter TypeScript configurations, you might encounter new compilation errors. Address them using TypeScript best practices.
- CSS Global Leakage (if switching to
ShadowDomorExperimentalIsolatedShadowDom): If you’re experimenting with new encapsulation strategies, be aware of how they might affect global styles.
Summary/Key Takeaways
- Preparation is Key: Backup, update dependencies, and run tests before upgrading.
- The
ng update @angular/core@21 @angular/cli@21command automates most of the migration process. - Review CLI output and manually update third-party libraries.
- Run all tests after migration to catch regressions.
- Gradually adopt new features, starting with those that are immediately beneficial and stable.
- Be deliberate about transitioning to fully zoneless, testing at each step of removing
Zone.jsdependencies. - Leverage the new optional migration scripts for
NgClass/NgStyleandCommonModule.
Migration to Angular v21 is generally smooth thanks to the robust tooling. By following this checklist and understanding the new features, you can confidently bring your applications to the cutting edge of Angular development.