如何在 MacOS 上更新 npm

由于我不知道的原因,我无法在 macOS 上更新到 npm 的最新版本(它在 Windows 上运行良好)。它没有显示任何错误,只显示‘ Updated1 package’

使用 Node.js 8.11.1

node -v
v8.11.1

我有什么版本的 npm?

$ npm -v
5.6.0

我试过了。

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

而且它无法更新。

$ npm -v
5.6.0

NPM 在哪?

$ which npm
/usr/local/bin/npm

所以我试了 brew

brew install npm

失败了。

$ npm -v
5.6.0

* And then I trying this... *

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

失败了。

npm -v
5.6.0

sudo:

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

失败了。

npm -v
5.6.0

这也失败了。

sudo npm install npm@latest -g

我遵循 在这个问答中找到的方向,从我的系统中完全删除 npm 和节点,并从头开始重新安装它们,它也无法更新。


截图,每个请求:

enter image description here


关闭终端,然后重新打开终端并运行:

$ npm -v
5.6.0

sudo两次:

$ 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

同样失败的还有:

$ npm -v
5.6.0

我错过了什么? 这是怎么回事?

175520 次浏览

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.)

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!!!

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"

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"

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"

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.

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.

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