107

For reasons unknown to me, I haven't been able to update to the latest version of npm on macOS (it works fine on Windows). It displays no error, only 'updated 1 package'.

Using Node.js 8.11.1

node -v
v8.11.1

What version of npm do I have?

$ npm -v
5.6.0

I tried this...

$ npm i -g npm
+ npm@5.8.0
updated 1 package in 7.37s

And it fails to update.

$ npm -v
5.6.0

Where is npm?

$ which npm
/usr/local/bin/npm

So I try brew...

brew install npm

And it fails...

$ npm -v
5.6.0

*And then I tried this... *

npm install npm@latest -g
+ npm@5.8.0
updated 1 package in 7.618s

And it fails...

npm -v
5.6.0

With sudo:

sudo npm i -g npm
+ npm@5.8.0
updated 1 package in 7.794s

And it fails...

npm -v
5.6.0

This also fails...

sudo npm install npm@latest -g

I followed the directions found on this Q&A, completely removing npm and node from my system and reinstalling them from scratch, and it also fails to update.


Screenshot, per request:

enter image description here


Close the terminal, and then re-open the terminal and running:

$ npm -v
5.6.0

sudo twice:

$ sudo npm i -g npm
+ npm@5.8.0
updated 1 package in 7.478s
$ sudo npm i -g npm
+ npm@5.8.0
updated 1 package in 7.434s

Also fails:

$ npm -v 
5.6.0

What did I miss? What's going on here?

NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
  • have you tried sudo npm i -g npm ? – Squish Apr 04 '18 at 22:43
  • @Squish yes, it was the first thing I tried, but I'll make that clearer. – NonCreature0714 Apr 04 '18 at 22:43
  • Try using sudo in that case. "sudo npm i -g npm" without the brackets – Squish Apr 04 '18 at 22:45
  • @Squish that doesn't work either – NonCreature0714 Apr 04 '18 at 22:47
  • what did the terminal say as an error? please specify – Squish Apr 04 '18 at 22:47
  • @Squish terminal displayed no error – NonCreature0714 Apr 04 '18 at 22:48
  • Try this thread on uninstalling and reinstalling the npm, maybe some data got corrupted. https://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x – Squish Apr 04 '18 at 22:52
  • @I followed that thread, completely removed node and npm, and reinstalled, and npm still won't update – NonCreature0714 Apr 04 '18 at 23:16
  • 1
    mind showing a screenshot of the terminal?, in this case I dont know what your up against(errors, warning, etc should be helpful). Try doing the "sudo npm install -g npm" twice.Also, try force quiting the terminal and reopening it for a refresh to take effect – Squish Apr 04 '18 at 23:21
  • @Squish tried re-opening terminal, and `sudo` twice: they both fail to update npm – NonCreature0714 Apr 04 '18 at 23:31
  • i have the same situation. Either that node installation came preinstalled or we used the installer from NodeJS.org and forgot about it. In any case, when you `ls -l /usr/local/bin/npm` you will see that it links to `../lib/node_modules/npm/bin/npm-cli.js`. Not sure whether and how to remove that but the $PATH should be changed to look up the brew version first – Cpt. Senkfuss Dec 08 '20 at 21:23
  • update: this answer finally got me to a PATH that contains the brew-installed node and npm: https://stackoverflow.com/a/44356455/1823536 – Cpt. Senkfuss Dec 09 '20 at 06:40

8 Answers8

150

This works on my mac.

Based on docs https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm :

npm install -g npm@latest

There is a note stated on the docs that depends on your installation method, you might addd some sudo.

Upgrading on *nix (OSX, Linux, etc.)

(You may need to prefix these commands with sudo, especially on Linux, or OS X if you installed Node using its default installer.)

marlo
  • 6,998
  • 5
  • 28
  • 34
  • 1
    for those curious, -g is the global install flag, as explained here: https://stackoverflow.com/questions/8909986/when-should-i-use-npm-with-g-flag-and-why – jeron Aug 24 '18 at 23:42
  • 9
    `sudo npm install -g npm@latest` works like magic – Abel Callejo Mar 08 '21 at 23:19
  • 7
    You should never install npm with sudo, it's only going to cause more permission headaches later on somewhere. There are many ways to install npm without sudo, brew being one of them, but google for many more. – redfox05 May 05 '21 at 13:00
  • Even though I installed node with brew, I could not upgrade it that way. Had to use sudo. – Noumenon Mar 01 '22 at 23:17
23

npm install -g npm@latest works fine!! and you can also replace the latest for specific versions like npm install -g npm@5.6.0

I hope it will help!!!

jithinkmatthew
  • 890
  • 8
  • 17
Juned Khan
  • 239
  • 2
  • 2
19

Perhaps you have already solved this, but here is what I found when I had exactly this issue. I had 2 versions of npm installed.

I verified this as follows:

grep \"version\" ~/.npm-packages/lib/node_modules/npm/package.json

  "version": "6.2.0"

grep \"version\" /usr/local/lib/node_modules/npm/package.json

  "version": "5.6.0",

I worked around the issue by updating the path in my bash profile, but would like to know why (how) I ended up with 2 versions. Here is the update:

tail -2 ~/.bash_profile

NPM_PACKAGES="${HOME}/.npm-packages"

PATH="$NPM_PACKAGES/bin:$PATH"
anothernode
  • 5,100
  • 13
  • 43
  • 62
doh
  • 222
  • 2
  • 3
  • 2
    I think people miss the point of what works about this solution. Remove the `/usr/local/lib` path and manage npm from .npm-packages, as it should be. – Raydot Oct 26 '18 at 01:53
  • 1
    after trying "this and that" I ended up installing from the nodejs.org package installer doing a custom install (only installing npm) – Alex Fuentes May 09 '19 at 09:15
  • Also make sure to have `:$PATH` at the end. In my case it was at the start and shell was using the wrong version. – user5365075 Jun 15 '21 at 22:11
16

In my case, only the following has helped:

sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
brew uninstall --ignore-dependencies node
brew install node

EDIT NOV 21: These days, I bypass brew entirely and use "n":

https://www.npmjs.com/package/n

And so I can change between versions too.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
15

In my case, none of the previous answers worked. For me, a working solution was a simple, five-step process.

  1. Make sure (the old version of) npm is installed.

npm -v

If npm is not installed, then install it on the Mac with Node.js.

  1. Globally installed the desired version of npm.
npm install -g npm@latest

This command uses the old version of npm (installed by Node), to globally install the latest version of npm at ~/.npm-global/. Once installed, close and open a new terminal shell.

  1. Remove the old version of npm installed by Node
rm -r /usr/local/lib/node_modules/npm/

Sometimes this doesn't work, so I had to go into finder to delete the /npm/ folder.

  1. Make sure to set the correct path variable.
echo $PATH

If ~/.npm-global/bin does not show up between the colons, then update the path variable. Open up ~/.zshrc in a text editor and add the following line. If you don't use zsh, open the profile for your corresponding shell (i.e. ~/.bash_profile)

export PATH=$PATH:$HOME/.npm-global/bin

Save your changes and close the text editor.

  1. Close and reopen the terminal shell and run npm -v to check that npm is correctly on the latest version.

The reason for updating the path variable is because the npm cli suggests you update npm with npm install -g npm which will install npm at a different location than where Node installs npm originally.

5

I faced the same problem. You might have already installed the npm version, and now it is time to point the new npm version install. You can do that by following below steps.

  1. sudo nano /usr/local/lib/node_modules/npm/package.json
  2. change "version" : "5.6.0" to "verison": "5.8.0"
Vihar Manchala
  • 667
  • 6
  • 14
  • Same issue here on MacOS - using `n` to run multiple node versions, but couldn't upgrade `npm` - this setting must manually be changed in the global `node_modules` package.config. Great find! – SliverNinja - MSFT Sep 29 '18 at 13:47
  • 2
    This just changes what version is reported on the CLI, not what version is actually being used. – Matt Molnar Mar 05 '19 at 18:44
  • Thanks, this works for me since I have more than one version of `npm` installed on my MacBook – Kris Stern Mar 09 '20 at 01:41
4

In my case, I'm using nvm to manage different versions of node. In order to upgrade npm version, I have to

1 - Install the latest version of npm by navigating to your current version of node

cd ~/.nvm/versions/node/v10.9.0
npm install npm

or you can probably use

nvm install-latest-npm

2 - Edit $PATH to point to your current version of node

NPM_PACKAGES="${HOME}/.nvm/versions/node/v10.9.0"
PATH="$NPM_PACKAGES/bin:$PATH"
Mr. 14
  • 9,228
  • 6
  • 37
  • 54
  • The first part of this answer, navigating to the current version that my vm was using and installing there is what worked for me. Like @Mr. 14 said, you go to the node directory, not all the way into the npm directory. so for me, the location of my npm install for the vm /usr/local/Cellar/node@8.11.3_1/lib/node_modules/npm (I'm forced to use an old version due to errors with 10) and I had to go to /usr/local/Cellar/node@8.11.3_1 and do npm install npm. Then it finally registered the latest version within my vm. – LeraA Sep 26 '18 at 14:43
-1

On my macOS the homebrew-installed npm did not want to upgrade due to pre-existing link file. I ran the install with --force flag and now things are okay.

$ npm --version
6.14.16
$ npm install -g npm@8
npm ERR! EEXIST: file already exists, symlink ...
$ npm install -g --force npm@8
npm WARN using --force I sure hope you know what you are doing.
+ npm@8.19.2
$ npm --version
8.19.2

Note also that in my case, no 'sudo' was required, thanks to homebrew. To check if your npm is installed with homebrew, use brew list.

$ brew list | grep node
node@12
Akseli Palén
  • 27,244
  • 10
  • 65
  • 75