How to Update a Node dependency from npm

Updated onbyAlan Morel
How to Update a Node dependency from npm

When you install Node, you also get npm, which is the official package manager for Node.

You can use npm to install dependencies that other people wrote for you to use in your project.

Over time, these dependencies get updates and you'll want to update them so keep up with the latest versions.

In this post, we'll learn how to update our dependencies with npm.

Check for outdated dependencies

Before we update our dependencies, we need to check if there are any outdated dependencies.

You can do this by running this command:

BASH
npm outdated

This command will list all the dependencies that are outdated. Simply run it in the root of your project, the same place where your package.json file is located.

Now that we know how to check for outdated dependencies, let's update our dependencies.

Updating outdated dependencies

You can update a specific dependency by running this command:

BASH
npm update <dependency>

For example, if you wanted to update the react dependency, you would run this command:

BASH
npm update react

If your package is installed globally, you will need to apply the --global flag to it:

BASH
npm update --global react

When you run any of these commands, npm will automatically update the dependency, package.json and your node_modules folder.

You can update all the dependencies at once by running this command:

BASH
npm update

By omitting the <dependency> argument, npm interprets it as you wanting to update all the dependencies.

Finally, you can choose what version specifically you want to update a dependency to by appending the version to it.

For example, here's how to update react to the latest version:

BASH
npm update react@latest

Conclusion

In this post, we learned how to update our dependencies with npm.

You can first verify which packages are outdated, then update them one by one, or all at once.

Hopefully this post has been helpful to you. Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.