How to update dependency to latest in nodejs|npm outdated update command


In My previous nodejs application, I have a need to convert all dependencies to latest. This blog talks about how to upgrade all your packages to latest version

This post solves developers questions while nodejs update latest packages

  • How do update each dependency in package.json
  • how to update all packages manually or automatically
  • how update global packages to latest versions
  • update dependencies and devdependencies
npm update and outdated command latest versions in nodejs

Nodejs project all dependencies maintained in package.json file

  "dependencies": {
   "package1:"version",
  },
    "devDependencies": {
       "package2:"version",

  },

So, we want to update dependencies and devDependencies sections with following commands

First check which package has older versions?

Any project which has to upgrade to latest versions has two things to do

  • Update latest version in package.json
  • Install the package using npm install, that means node_modules updated with latest version and package-lock.json file is updated

Let’s discuss in next section

Findout outdate package versions in nodejs project

Node has inbuilt command ‘npm outdated’ to list out all old versions in an project.

My Project is using Angular and primeng with older versions,

Let’s run the command in root directory

B:\angular-primeng-chart-examples>npm outdated
Package                            Current  Wanted  Latest  Location
@angular/animations                 11.1.0  9.1.13  11.1.0  angular
@angular/cdk                        11.1.0   9.2.4  11.1.0  angular
@angular/common                     11.1.0  9.1.13  11.1.0  angular
@angular/compiler                   11.1.0  9.1.13  11.1.0  angular
@angular/core                       11.1.0  9.1.13  11.1.0  angular
@angular/forms                      11.1.0  9.1.13  11.1.0  angular
@angular/platform-browser           11.1.0  9.1.13  11.1.0  angular
@angular/platform-browser-dynamic   11.1.0  9.1.13  11.1.0  angular
@angular/router                     11.1.0  9.1.13  11.1.0  angular
@fullcalendar/core                   5.5.1   4.4.2   5.5.1  angular
primeicons                           4.1.0   2.0.0   4.1.0  angular
primeng                             11.2.0   9.1.3  11.2.0  angular
tslib                              MISSING  1.14.1   2.1.0  angular
zone.js                             0.11.3  0.10.3  0.11.3  angular

npm outdated checks following things

  • It checks each package in a project and gives latest versions to console. However, this is updated to package.json.
  • It gives current installed version and latest version to upgrade.
  • And also list wanted version which is recommended version to upgrade

Is it safe to run npm updated command to latest versions?

Yes, All installed packages or dependencies are safe to upgrade to wanted version,However, latest recommendations are not recommended

update single or all packages to latest versions

npm update command updates package to latest versions in package.json

How to update single package to latest version?

npm update package

It installs package to latest versions.

Run below command, to save and update latest package

npm update --save package

that means, This updates latest version to package.json in an project,

current version -
After running

npm update --save primeng

This updates latest version in package.json

dependencies:{
 "primeng": "11.2.0",
 }

Next step, install primeng in node_modules and updates package-lock.json file

npm install

update all packages to latest version with npm update

There are multiple ways update all package with single command

First, npm update command

npm update

This check each package and update to latest version in package.json, However this does not update devDependencies to latest versions.

How do you update devDependencies to latest versions?

Next is to install using npm install command.

With this, Manual update is not required, However, this breaks the application and not to safe to use.

Second, npm-check-updates tool

Install npm-check-updates globally using npm install

npm install npm-check-updates -g

Next is to update packages in single command

ncu -u

This will update dependencies and devDependencies to latest versions.

update all global packages of a nodejs project

As we discuss above, npm outdated gives local packages of an nodejs project.

If you want to check global versions, Then -g option of npm outdated command.

npm outdated -g

And, the result is list out global packages with current,wanted and latest versions.

Next, update command with -g option updates package.json file

npm update -g 

Conclusion

This tutorials covered multiple approaches ways to update versions

  • npm outdated list out existing local versions of an nodejs application
  • npm outdated -g specified global versions of nodejs environment
  • npm update or (-g) updates single or multiple dependencies of an local or global packages
  • ncu command updates latest version, npm install installs updated dependencies
Similar Posts