How to Upgrade from Angular 2 to Angular 5 Apps

This blog represents steps required to upgrade or update or migrate existing Angular 2 apps to Angular 5 apps. Greater details can be found on Angular 5 announcement blog.

Upgrade/Update Preparation from Angular 2 to Angular 5 Apps

The following are some of the activities which are recommended before the update/upgrade is done:

  • Ensure that extends with lifecycle events are replaced with implements <lifecycle event>
  • In case animation is used in the app, BrowserAnimationsModule from @angular/platform-browser/animations needs to be imported in NgModule
  • OpaqueTokens should be replaced with InjectionTokens
  • Rename the template tags to ng-template tag
  • Update package.json file appropriately to update dependencies and devDependencies

Update Package.json file for Dependencies/DevDependencies

It is important to update package.json file for upgrading angular apps using Angular 4 to use Angular 5 modules. Once updated, later section discusses how to install latest modules.

Pay attention to the changes which needed to be made in package.json:

  • Update dependencies versions
    • Angular modules version 2.* needs to be changed to ^5.0.0 or ^5.x.x (based upon whatever is relevant)
    • Update versions of rxjs to ^5.5.2
  • Update devDependencies versions
    • @angular/cli (^1.5.0)
    • @angular/compiler-cli (^5.0.0)
    • @angular/language-service (^5.0.0)
    • codelyzer (~3.2.0)
    • tslint (~5.7.0)
    • typescript (~2.4.2)

After making the above changes, this is how the code snippet should look like:

"dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  "devDependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2",    

Run Upgrade/Update: Run NPM Install

Perform following steps to reinstall the angular and related modules:

  • Execute the following command to reinstall node modules
    npm install

Post Update/Upgrade Activities

The following are some of the activities which can be done post update/upgrade:

  • Any animations services/tools imported from imported from @angular/core should be rather imported from @angular/animations.
  • Switch from Http Service to HttpClient Service
  • Usage of rxjs/operators import

Switch from Http Service to HttpClient Service

It is recommended to switch from HttpModule and the Http service to HttpClientModule and the HttpClient service. In version 4.3, HttpClient was shipped as part of @angular/common module. This saw a lot of adoption in Angular developers. Thus, Angular is promoting HttpClient and decided to deprecate Angular Http service which was part of HttpModule.

In order to update to HttpClient, all that is required to be done are some of the following:

  • Replace HttpModule with HttpClientModule from @angular/common/http in each of the modules,
  • Inject the HttpClient service
  • Remove any map(res => res.json()) calls

Usage of rxjs/operators import

For each RxJS operator you import, use import from ‘rxjs/operators’ and use the pipe operator.

Start Angular 5 App

  • Start the app using command such as npm start, ng serve or ng serve –open. And, you should be able to access your app.

While starting the app, you may run through following error:

Cannot find module ‘webpack/lib/node/NodeTemplatePlugin’

The following could be done to get away with the above error:

npm remove webpack -g
npm i webpack --save-dev

Feel free to check this angular apps migration page by Google. In case you are developing web apps using Spring and Angular, check out my book, Building web apps with Spring 5 and Angular. Grab your ebook today and get started.

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in AngularJS, Javascript, Web. Tagged with , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *