How to keep your npm dependencies updated
It's always a good idea to keep dependencies updated, from performance gains to fixing security issues, it just makes sense to update.
But your project might depend on a lot of dependencies and if you haven't worked on a project for a few months, chances are a lot of these dependencies are really outdated.
List all outdated dependencies
You can run the command
npm outdated to get a list of all the dependencies that have new releases.
Check for updates
Now that we know how to check for outdated packages, we need to find a way to update them all. This is where we need help from a package called
npm-check-updates. This package will look into your
package.json versions and upgrade all the outdated ones.
npm install -g npm-check-updates
Note: You should install this package globally so you can use on other projects.
Upgrading and updating dependencies
After installing the package you need to run
ncu -u on your terminal to upgrade your package.json.
You can now run the command
npm update to update your dependencies.
Updating dependencies in one go
So we have seen that we can upgrade our dependencies with
ncu -u and then upgrade them with
npm update. How about we create a command that will run both of these so we don't have to type so much?
That's when aliases come in handy!
I keep all my aliases in a
.bash_aliases file, in my home directory. You can add this alias anywhere, for example,
~/.bash_profile, etc. If you don't know which to use, you can use
Open the file in your favourite editor and add the following alias:
alias update='ncu -u && npm update'
Source that file or restart your terminal and you can now use the
update command to update your dependencies in one go!