Hỏi đáp

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, npm and 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 npm

Please note that this command will remove your current version of npm. Make sure to use sudo npm install -g npm if on a Mac.

You can also update all outdated local packages by doing npm update without any arguments, or global packages by doing npm 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 nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages 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 node is 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.

Bài viết liên quan

Back to top button