如何使用nvm更改npm版本?

时间:2021-05-30 07:27:25

I've been using NVM to install the latest versions of nodeJS for my node work. It works totally fine for installing separate versions and switching between them. It also installs the latest version of NPM within each local .../bin folder along with the node binary. However, there doesn't seem to be any way to switch the version of NPM that I'm using (or at least I can't figure it out).

我一直在使用NVM为我的节点工作安装nodeJS的最新版本。它完全适用于安装不同的版本并在它们之间切换。它还在每个本地安装最新版本的NPM…/bin文件夹以及节点二进制文件。然而,似乎没有任何方法可以改变我正在使用的NPM版本(或者至少我搞不清楚)。

The only solution I can think of myself is to delete the binary that it's defaulting to (which is the NPM that was installed when I first installed node with NVM), and in its place to put the latest NPM binary. However, I'm wondering if there are any better ways to go about doing this.

我能想到的唯一解决方案是删除它默认使用的二进制文件(这是我第一次使用NVM安装节点时安装的NPM),并放置最新的NPM二进制文件。然而,我想知道是否有更好的方法去做这件事。

10 个解决方案

#1


211  

Over three years after this question was first asked, it seems like the answer is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

在这个问题被首次提出三年后,现在的答案似乎简单多了。只需更新nvm安装的版本,它位于~/.nvm/version /node/[your-version]/lib/node_modules/npm中。

I just installed node 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:

我刚刚安装了节点4.2.2,这是npm 2.14.7,但我想使用npm 3。所以我做了:

cd ~/.nvm/versions/node/v4.2.2/lib
npm install npm

Easy!

简单!

And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node. (EDIT: In the newest version, npm -g is smart and installs modules into the path above instead of the system global path.)

是的,这应该适用于任何模块,而不仅仅是npm,您希望为特定版本的节点“全局”。(编辑:在最新版本中,npm -g是智能的,将模块安装到上面的路径中,而不是系统全局路径中。)

#2


36  

nvm doesn't handle npm.

npm nvm不处理。

So if you want to install node 0.4.x (which many packages still depend on) and use NPM, you can still use npm 1.0.x.

如果你想安装节点0。4。x(许多包仍然依赖它)和使用NPM,您仍然可以使用NPM 1.0.x。

Install node 0.6.x (which comes with npm 1.1.x) and install nvm with npm:

安装节点0.6。x (npm 1.1.x附带)和npm安装nvm:

npm install nvm
. ~/nvm/nvm.sh

Install node 0.4.x with nvm:

安装节点0.4。x nvm:

nvm install v0.4.12
nvm use v0.4.12

Install npm using install.sh (note the -L param to follow any redirects):

使用安装安装npm。sh(注意-L参数以跟踪任何重定向):

curl -L https://npmjs.org/install.sh | sh

This will detect node 0.4.12 and install npm 1.0.106 in your ~/nvm/v0.4.12/lib/node_modules folder and create symlink for nvm

这将检测节点0.4.12并在您的~/nvm/v0.4.12/lib/node_modules文件夹中安装npm 1.0.106,并为nvm创建符号链接

~/nvm/v0.4.12/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

If you try to run npm, it will still give an error but if you do nvm use v0.4.12 again, it should now work.

如果您尝试运行npm,它仍然会给出一个错误,但是如果您使用nvm再次使用v0.4.12,那么它现在应该可以工作了。

#3


15  

Changing npm versions on linux based OSs isn't a straight forward one command process yet. I have done following to switch back to older version of npm. This should work to get any version of npm working. First install the version of npm you want to use:

在基于OSs的linux上更改npm版本还不是一个直接的命令过程。我已经做了以下的转换回到老版本的npm。这应该可以使任何版本的npm工作。首先安装您想要使用的npm版本:

sudo npm install -g npm@X.X.X

Remove the sym link in /usr/local/bin/

删除/usr/local/bin/中的sym链接

sudo rm /usr/local/bin/npm

Recreate the sym link using the desired version of npm you have installed

使用已安装的npm的期望版本重新创建sym链接

sudo ln -s /usr/bin/npm@X.X.X /usr/local/bin/npm

#4


13  

npm install npm@x.x.x -g
npm install npm@5.4.0 -g

#5


8  

By looking at www.npmjs.com/install.sh I found there is a way to install a specific version by setting an environment-variable

通过查看www.npmjs.com/install.sh,我发现可以通过设置环境变量来安装特定的版本

export npm_install="2.14.14"

出口npm_install = " 2.14.14 "

Then run the download-script as described at npmjs.com:

然后按照npmjs.com的描述运行下载脚本:

curl -L https://www.npmjs.com/install.sh | sh

curl -L https://www.npmjs.com/install.sh | sh

If you omit setting the npm_install variable, then it will install the the version they have marked as latest

如果您忽略了设置npm_install变量,那么它将安装他们标记为最新的版本

#6


8  

nvm now has a command to update npm. It's nvm install-latest-npm or npm install --latest-npm.

nvm现在有一个更新npm的命令。它是nvm最新的npm或最新的npm安装。

#7


4  

In windows, run your terminal as admin (in case there are permission issues as I had). Then use a specific node version (say 7.8.0) by

在windows中,以admin的身份运行终端(如果像我一样存在权限问题)。然后使用特定的节点版本(如7.8.0)by

nvm use 7.8.0

then update your npm to desired specific version by

然后更新您的npm到需要的特定版本。

npm install -g npm@5.0.3

#8


1  

Slight variation on the above instructions, worked for me. (MacOS Sierra 10.12.6)

上面的说明稍微有些变化,对我有用。(MacOS塞拉10.12.6)

npm install -g npm@3.10.10
rm /usr/local/bin/npm
ln -s ~/.npm-packages/bin/npm /usr/local/bin/npm
npm --version

#9


0  

What about npm i -g npm? Did you try to run this as well?

那么npm我-g npm呢?你也试过运行这个吗?

#10


0  

I'm on Windows and I couldn't get any of this stuff to work. I kept getting errors about files being in the way. This worked though:

我在Windows系统上,我无法让这些东西发挥作用。我不断地收到关于文件被挡在路上的错误信息。不过这个工作:

cd C:\Users\ryans\AppData\Roaming\nvm\v8.10.0           # or whatever version you're using
mv npm npm-old
mv npm.cmd npm-old.cmd
cd node_modules\
mv npm npm-old
cd npm-old\bin
node npm-cli.js i -g npm@latest

And boom, I'm back in business.

然后,我又开始做生意了。

#1


211  

Over three years after this question was first asked, it seems like the answer is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

在这个问题被首次提出三年后,现在的答案似乎简单多了。只需更新nvm安装的版本,它位于~/.nvm/version /node/[your-version]/lib/node_modules/npm中。

I just installed node 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:

我刚刚安装了节点4.2.2,这是npm 2.14.7,但我想使用npm 3。所以我做了:

cd ~/.nvm/versions/node/v4.2.2/lib
npm install npm

Easy!

简单!

And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node. (EDIT: In the newest version, npm -g is smart and installs modules into the path above instead of the system global path.)

是的,这应该适用于任何模块,而不仅仅是npm,您希望为特定版本的节点“全局”。(编辑:在最新版本中,npm -g是智能的,将模块安装到上面的路径中,而不是系统全局路径中。)

#2


36  

nvm doesn't handle npm.

npm nvm不处理。

So if you want to install node 0.4.x (which many packages still depend on) and use NPM, you can still use npm 1.0.x.

如果你想安装节点0。4。x(许多包仍然依赖它)和使用NPM,您仍然可以使用NPM 1.0.x。

Install node 0.6.x (which comes with npm 1.1.x) and install nvm with npm:

安装节点0.6。x (npm 1.1.x附带)和npm安装nvm:

npm install nvm
. ~/nvm/nvm.sh

Install node 0.4.x with nvm:

安装节点0.4。x nvm:

nvm install v0.4.12
nvm use v0.4.12

Install npm using install.sh (note the -L param to follow any redirects):

使用安装安装npm。sh(注意-L参数以跟踪任何重定向):

curl -L https://npmjs.org/install.sh | sh

This will detect node 0.4.12 and install npm 1.0.106 in your ~/nvm/v0.4.12/lib/node_modules folder and create symlink for nvm

这将检测节点0.4.12并在您的~/nvm/v0.4.12/lib/node_modules文件夹中安装npm 1.0.106,并为nvm创建符号链接

~/nvm/v0.4.12/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

If you try to run npm, it will still give an error but if you do nvm use v0.4.12 again, it should now work.

如果您尝试运行npm,它仍然会给出一个错误,但是如果您使用nvm再次使用v0.4.12,那么它现在应该可以工作了。

#3


15  

Changing npm versions on linux based OSs isn't a straight forward one command process yet. I have done following to switch back to older version of npm. This should work to get any version of npm working. First install the version of npm you want to use:

在基于OSs的linux上更改npm版本还不是一个直接的命令过程。我已经做了以下的转换回到老版本的npm。这应该可以使任何版本的npm工作。首先安装您想要使用的npm版本:

sudo npm install -g npm@X.X.X

Remove the sym link in /usr/local/bin/

删除/usr/local/bin/中的sym链接

sudo rm /usr/local/bin/npm

Recreate the sym link using the desired version of npm you have installed

使用已安装的npm的期望版本重新创建sym链接

sudo ln -s /usr/bin/npm@X.X.X /usr/local/bin/npm

#4


13  

npm install npm@x.x.x -g
npm install npm@5.4.0 -g

#5


8  

By looking at www.npmjs.com/install.sh I found there is a way to install a specific version by setting an environment-variable

通过查看www.npmjs.com/install.sh,我发现可以通过设置环境变量来安装特定的版本

export npm_install="2.14.14"

出口npm_install = " 2.14.14 "

Then run the download-script as described at npmjs.com:

然后按照npmjs.com的描述运行下载脚本:

curl -L https://www.npmjs.com/install.sh | sh

curl -L https://www.npmjs.com/install.sh | sh

If you omit setting the npm_install variable, then it will install the the version they have marked as latest

如果您忽略了设置npm_install变量,那么它将安装他们标记为最新的版本

#6


8  

nvm now has a command to update npm. It's nvm install-latest-npm or npm install --latest-npm.

nvm现在有一个更新npm的命令。它是nvm最新的npm或最新的npm安装。

#7


4  

In windows, run your terminal as admin (in case there are permission issues as I had). Then use a specific node version (say 7.8.0) by

在windows中,以admin的身份运行终端(如果像我一样存在权限问题)。然后使用特定的节点版本(如7.8.0)by

nvm use 7.8.0

then update your npm to desired specific version by

然后更新您的npm到需要的特定版本。

npm install -g npm@5.0.3

#8


1  

Slight variation on the above instructions, worked for me. (MacOS Sierra 10.12.6)

上面的说明稍微有些变化,对我有用。(MacOS塞拉10.12.6)

npm install -g npm@3.10.10
rm /usr/local/bin/npm
ln -s ~/.npm-packages/bin/npm /usr/local/bin/npm
npm --version

#9


0  

What about npm i -g npm? Did you try to run this as well?

那么npm我-g npm呢?你也试过运行这个吗?

#10


0  

I'm on Windows and I couldn't get any of this stuff to work. I kept getting errors about files being in the way. This worked though:

我在Windows系统上,我无法让这些东西发挥作用。我不断地收到关于文件被挡在路上的错误信息。不过这个工作:

cd C:\Users\ryans\AppData\Roaming\nvm\v8.10.0           # or whatever version you're using
mv npm npm-old
mv npm.cmd npm-old.cmd
cd node_modules\
mv npm npm-old
cd npm-old\bin
node npm-cli.js i -g npm@latest

And boom, I'm back in business.

然后,我又开始做生意了。