Tuesday, October 16, 2018

npm-check-updates npm package - update dependencies in package.json|bower.json

In this blog post, We are going to learn npm-check-updates npm package to update dependencies in package.json and bower.json.

npm-check-updates npm package 

npm check updates package is used to check all dependencies in package.json of a node js project and update dependencies to latest versions. This package works by updating following configuration files

  • package.json 
  • bower.json

This library solves all below problems of a NodeJS application
  • How to update all dependencies in package.json to latest versions?\
  • How to update devDependencies in package.json to latest versions?
  • Update dependencies in bower.json to the latest version

Setup npm-check-updates or ncu package globally

node and npm command should work before install npm-check-updates. Please install npm-check-updates globally using npm install command. Once this is installed, ncu command is available
npm i -g npm-check-updates
B:\Workspace\blog\angularapp>ncu --version
2.14.1

How to check outdated dependency in package.json 

To update any packages to latest versions, First, you need to check outdated old version, Please issue npm outdated command The below example checks outdated dependencies in angular package.json
B:\Workspace\blog\angularapp>npm outdated
Package                        Current  Wanted   Latest  Location
@angular-devkit/build-angular    0.7.5   0.7.5    0.8.5  angularapp
@angular/cli                     6.1.5   6.1.5    6.2.5  angularapp
@types/node                      8.9.5   8.9.5  10.12.0  angularapp
codelyzer                        4.2.1   4.2.1    4.5.0  angularapp
jasmine-core                    2.99.1  2.99.1    3.2.1  angularapp
karma                            1.7.1   1.7.1    3.0.0  angularapp
karma-jasmine-html-reporter      0.2.2   0.2.2    1.3.1  angularapp
protractor                       5.3.2   5.3.2    5.4.1  angularapp
ts-node                          5.0.1   5.0.1    7.0.1  angularapp
tslint                           5.9.1   5.9.1   5.11.0  angularapp
typescript                       2.7.2   2.7.2    3.1.3  angularapp

How to check dependencies and update to latest versions?

First list out all the package with current versions and latest versions of package.json
B:\Workspace\blog\angularapp>ncu
Using B:\Workspace\blog\angularapp\package.json
[..................] \ :
 @angular-devkit/build-angular   ~0.7.0  →    ~0.8.5
 @angular/cli                    ~6.1.1  →    ~6.2.5
 @types/node                     ~8.9.4  →  ~10.12.0
 codelyzer                       ~4.2.1  →    ~4.5.0
 jasmine-core                   ~2.99.1  →    ~3.2.1
 karma                           ~1.7.1  →    ~3.0.0
 karma-jasmine-html-reporter     ^0.2.2  →    ^1.3.1
 protractor                      ~5.3.0  →    ~5.4.1
 ts-node                         ~5.0.1  →    ~7.0.1
 tslint                          ~5.9.1  →   ~5.11.0
 typescript                      ~2.7.2  →    ~3.1.3

Run ncu with -u to upgrade package.json
This will only just displays the result to console and not updated package.json Next update each dependency in package.json to latest versions
B:\Workspace\blog\angularapp>ncu -u
Using B:\Workspace\blog\angularapp\package.json
[..................] \ :
 @angular-devkit/build-angular   ~0.7.0  →    ~0.8.5
 @angular/cli                    ~6.1.1  →    ~6.2.5
 @types/node                     ~8.9.4  →  ~10.12.0
 codelyzer                       ~4.2.1  →    ~4.5.0
 jasmine-core                   ~2.99.1  →    ~3.2.1
 karma                           ~1.7.1  →    ~3.0.0
 karma-jasmine-html-reporter     ^0.2.2  →    ^1.3.1
 protractor                      ~5.3.0  →    ~5.4.1
 ts-node                         ~5.0.1  →    ~7.0.1
 tslint                          ~5.9.1  →   ~5.11.0
 typescript                      ~2.7.2  →    ~3.1.3

Upgraded B:\Workspace\blog\angularapp\package.json
This will updates dependencies versions to latest versions in package.json. Here is updated package.json files
{
  "name": "angularapp",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.1.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "angular2-uuid": "^1.1.1",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.8.5",
    "@angular/cli": "~6.2.5",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~10.12.0",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~3.2.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^1.3.1",
    "protractor": "~5.4.1",
    "ts-node": "~7.0.1",
    "tslint": "~5.11.0",
    "typescript": "~3.1.3"
  }
}

How to update dependencies to latest versions in bower.json 

Like a package.json package update, We can use ncu command with option -m
ncu -m bower.json

Related article


EmoticonEmoticon