Npm update all dependencies in NodeJS


In this post we will learn about how to npm update allย packages to latest version in Node Js.

In nodejs, when we install a package using npm install <package-name> , it downloads the latest version of the package and put it in the node_modules folder. It also create entries in package.json and package-lock.json file with the install package versions.

For example, lets suppose we install axios usingnpm install axios , now an entry is made in package.json file.

{
  "dependencies": {
    "axios": "^0.21.4",
  }
}

In the package.json file, you can see the name axios along with its version. The ^ sign means that npm can update to minor or the patch version when release.

NOTE: Package updates can came in three form:

  1. patch : If there are some bug fixes etc.
  2. minor : If new features are added to the package.
  3. major : If something new is added to that package which may break the existing code.

Npm update dependencies to minor or patch release

If you want to discover new updates of the packages in your node project just type:

npm outdated

npm-check-updates

Whenever there is a new patch or minor update is release, you need to type :

npm update

It will update packages and its dependencies to the latest version in your project.

Npm update dependencies to major release (Breaking Changes)

When there is some major update release for any packages, npm update won't update it to the major release. It is because major updates can bring some breaking changes to your project and npm tries to prevent you from all the trouble.

However if you need to update dependencies of your package.json file to the major update version, follow the steps below:

STEP 1: First, you need to install the package npm-check-updates globally:

npm install -g npm-check-updates

STEP 2: And now run the command:

ncu -u

This command will update the version numbers of the packages and its dependencies in your package.json file. So that npm can install the latest major version.

STEP 3: Now just run the update command:

npm update

This will updates all the package and its dependencies of the project to the major update versions.

Or, you can run the npm installcommand, if you have just clone or downloaded the project and don't have the node_modules folder.

Conclusion : Its better to just update your dependencies to patch release or minor updates because it safer and don't bring any breaking change.

Update your packages to major release if it's an absolute necessary.

Related Topics:

Resolve Npm Cannot Find Module Error In Nodejs

How To Clear Cache In Npm?

Add Node_module Folder To .Gitignore File

Npm WARN Package.Json: No Repository Field


๐Ÿ’พ Recommended Articles