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:
BASHnpm 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:
BASHnpm update <dependency>
For example, if you wanted to update the react
dependency, you would run this command:
BASHnpm update react
If your package is installed globally, you will need to apply the --global
flag to it:
BASHnpm 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:
BASHnpm 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:
BASHnpm 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!
- Getting Started with TypeScript
- Getting Started with Solid
- Getting Started with Electron
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- Learn how to use v-model with a custom Vue component
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting User Location using JavaScript's Geolocation API
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications
- Setting Up Stylus CSS Preprocessor