{

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


update dependency to latest in nodejs examples npm outdated

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

This post solves developers' questions while nodejs update the latest packages

  • How do update each dependency in package.json
  • how to update all packages manually or automatically
  • how to update global packages to the 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 the following commands

First, check which package has older versions?

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

  • Update the latest version in package.json

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

    Let’s discuss this in the next section

Find out outdated package versions in the nodejs project

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

My Project is using Angular and primeng with older versions,

Let’s run the command in the 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 the latest versions to the console. However, this package.json is updated.
  • It gives the currently installed version and the latest version to upgrade.
  • And also list the wanted version which is recommended version to upgrade

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

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

update single or all packages to the latest versions

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

How to update a single package to the latest version?

npm update package

It installs the package to the latest versions.

Run the below command, to save and update the latest package

npm update --save package 

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

current version - After running

npm update --save primeng

This updates the latest version in package.json

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

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

npm install

update all packages to the latest version with npm update

There are multiple ways to update all packages with a single command

First, the npm update command

npm update

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

How do you update devDependencies to the latest versions?

Next is to install using the npm install command.

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

Second, the npm-check-updates tool

Install npm-check-updates globally using npm install

npm install npm-check-updates -g

Next is to update packages in a single command

ncu -u

This will update dependencies and devDependencies to the latest versions.

update all global packages of a nodejs project

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

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

npm outdated -g

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

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

npm update -g 

Conclusion

These tutorials covered multiple approaches and ways to update versions

  • npm outdated list out existing local versions of a nodejs application
  • npm outdated -g specified global versions of nodejs environment
  • npm update or (-g) updates single or multiple dependencies of local or global packages
  • ncu command updates the latest version, npm install installs updated dependencies
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.