How to update npm using nvm

If you are JavaScript developer and not using nvm you might want to look at it. Node Version Manager is easy way to install, manage and work with multiple node versions.

Recently I got npm update notification, that looks something like this:

npm update notification

For some reason, when I had node installed through nvm, npm i -g npm didn't work. However, when I was using a "system" node version, installed from Node.js website, it did. I can't remember the exact error, but it was probably something with my PATH configuration.

So I tried to find a path where global packages are installed when using nvm. Once I found it, it was super easy to update npm in that specific folder.

# navigate to nvm's node lib folder
# (replace v8.4.0 with your version)
cd ~/.nvm/versions/node/v8.4.0/lib/

# update npm right there
npm install npm

# reopen your terminal

That's it. Great thing is that npm can update itself. And this works for any other global npm package.

Or, as suggested in the comments, just use nvm's command:

nvm install-latest-npm

Comments (8)

Ethan
17. Jun 2018, 16:09

Easier way is simply to do an npm global install, which would automatically use the correct path to the current nvm version in use.

npm install -g npm
Shawn Erquhart
23. Oct 2018, 16:25

Turns out nvm does have a command for this (nvm/issues/1541#issuecomment-377826880):

nvm install-latest-npm

Not sure why it isn't documented.

Stanko
23. Oct 2018, 16:40

Heh that is awesome, thanks Shawn!

C. L. Jones
14. Dec 2019, 20:47

Nice find, Shawn! Exactly what I needed :).

Zafer
25. Jan 2020, 18:30

Great tip. Thanks.

Élisson Michael
28. Jan 2020, 18:00

Thank You for Sharing This.

Jordan Harband
07. Nov 2020, 08:58

Hi! I maintain nvm, and "nvm doesn’t let you update only npm" is utterly incorrect. nvm does not have any impact on your ability to update npm, except that it removes the need to use sudo.

npm install -g npm always works, and if it doesn't work for you, please file an issue on nvm's repo, because you'll be the first person on the planet to report it.

Stanko
07. Nov 2020, 09:25

Hey Jordan,

First I want to thank you for working on nvm, it is a life saver!

I encountered this issue with updating npm while using nvm in 2017, and npm i -g npm didn't work for some reason. However, it did work when I had a single node version installed from Node.js website. And I remember seeing quite a few people with the same problem on stack overflow.

That is why I wrongly accused nvm, and I'm sorry for that, I will update the post to reflect this.

Cheers!