How to upgrade all packages in JS or NodeJS?

How to upgrade all packages in JS or NodeJS?
Photo by Claudio Schwarz / Unsplash

Javascript ecosystem is one of the finest and most evolving platform in the programming community. Hence you would find a lot of times the packages you have used in your project or in the global space get outdated soon. It becomes a task to then go and find out the upgrades and manually update the packages.

Though one should always read the change-logs of the newly released package and then migrate to the newer version, but still knowing the commands to identify the outdated packages is important.

How to find all the outdated packages in your global space?

npm outdated -g --depth=0

This would find all the outdated npm packages installed in your system in the global space, also it gives you the newer version on which you can upgrade.

How to find all the outdated packages in your local space?

npm outdated

PS- Make sure to run this command in the directory where the code resides.

How to update all globally outdated packages?

npm update -g

This would update all the globally outdated packages to the latest versions which were recommended by the previous command.

How to update a single package globally?

npm update -g <package-name>@<version>

How to update all packages in a local repository?

Go to the project directory and then run the following command
npm update

Updating packages downloaded from the registry | npm Docs
Documentation for the npm registry, website, and command-line interface

The above steps are helpful for when we use NPM. But when you use YARN then the steps to upgrade to the latest package are much simpler. Just use the following command.

yarn upgrade-interactive --latest

You would be prompted for upgrading all the packages as per the latest versions available.
PS- Please note, yarn only works when you have yarn.lock file, which is generated when you use yarn in your project.