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:
- patch : If there are some bug fixes etc.
- minor : If new features are added to the package.
- 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
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 install
command, 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: