How can I update Node.js and npm to their latest versions?
I just installed Node.js and npm. I installed npm for access to additional Node.js modules. After I installed Node.js and npm, I noticed that neither were the latest versions available.
So my questions are:
- How do I upgrade Node.js,
npmand my Node.js modules to their latest versions? - Do I need to uninstall Node.js and
npm, and reinstall the latest versions?
Answers:
How to update npm
Use the following command:
npm update -g npm
See the documentation for the update command:
npm update [-g] [<pkg>...]This command will update all the packages listed to the latest version (specified by the tag config), respecting semver.
Additionally, see the documentation on Node.js and npm installation and Upgrading npm.
The following original answer is from the old FAQ that no longer exists, but it should work for Linux and Mac:
How do I update npm?
npm install -g npmPlease note that this command will remove your current version of npm. Make sure to use
sudo npm install -g npmif on a Mac.You can also update all outdated local packages by doing
npm updatewithout any arguments, or global packages by doingnpm update -g.Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already. (Consider, if there is ever a bug in the update command.) In those cases, you can do this:
curl https://www.npmjs.com/install.sh | sh
How to update Node.js
To update Node.js itself, I recommend you use nvm (Node Version Manager). Here is the quote from the official npm documentation:
We strongly recommend using a Node version manager like
nvmto install Node.js andnpm. We do not recommend using a Node installer, since the Node installation process installsnpmin a directory with local permissions and can cause permissions errors when you runnpmpackages globally.
Here is how you can do it using nvm tool:
-
To install the latest release of Node, do this:
nvm install node(NOTE: here
nodeis an alias for the latest version) -
To install a specific version of node:
nvm install 22.2.0
More details can be found on the official nvm GitHub page in the “Usage” section.