node.js 如何使用 nvm 更改 npm 的版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9755841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:21:02  来源:igfitidea点击:

How can I change the version of npm using nvm?

node.jsnpm

提问by thisissami

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。它非常适合安装单独的版本并在它们之间切换。它还在每个本地 .../bin 文件夹中安装最新版本的 NPM 以及节点二进制文件。但是,似乎没有任何方法可以切换我正在使用的 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 二进制文件。但是,我想知道是否有更好的方法来做到这一点。

回答by lawrence

EDIT: several years since this question was first answered, as noted in a newer answer, there is now a command for this:

编辑:自从第一次回答这个问题几年后,如较新的答案所述,现在有一个命令:

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

nvm 现在有一个命令来更新 npm。它是nvm install-latest-npmnvm install --latest-npm

nvm install-latest-npm: Attempt to upgrade to the latest working npmon the current node version

nvm install-latest-npm:尝试升级到npm当前节点版本的最新工作

nvm install --latest-npm: After installing, attempt to upgrade to the latest working npm on the given node version

nvm install --latest-npm:安装后,尝试在给定节点版本上升级到最新的工作 npm

Below are previous revisions of the correct answer to this question.

以下是此问题正确答案的先前修订版。

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/versions/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:

我刚刚安装了 node 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.

是的,这应该适用于任何模块,而不仅仅是 npm,您希望为特定版本的节点“全局”。



EDIT 1: In the newest version, npm -gis smart and installs modules into the path above instead of the system global path.

编辑 1:在最新版本中,npm -g是 smart 并将模块安装到上面的路径而不是系统全局路径中。



Thanks @philraj for pointing this out in a comment.

感谢@philraj 在评论中指出这一点。

回答by fijiaaron

nvm doesn't handle npm.

nvm 不处理 npm。

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.

所以如果你想安装 node 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:

安装 node 0.6.x(随 npm 1.1.x 一起提供)并使用 npm 安装 nvm:

npm install nvm
. ~/nvm/nvm.sh

Install node 0.4.x with nvm:

使用 nvm 安装节点 0.4.x:

nvm install v0.4.12
nvm use v0.4.12

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

使用 install.sh 安装 npm(注意-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.12again, it should now work.

如果您尝试运行 npm,它仍然会出错,但是如果您nvm use v0.4.12再次运行,它现在应该可以工作了。

回答by samlandfried

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

nvm 现在有一个命令来更新 npm。它是nvm install-latest-npmnpm install --latest-npm

回答by Ocko

npm install [email protected] -g
npm install [email protected] -g

回答by Ryan Shillington

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 %APPDATA%\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

cd %APPDATA%\nvm\v8.10.0 # or whatever version you're using
rm npm-old
rm npm-old.cmd
cd node_modules\
rm -rf npm-old

And boom, I'm back in business.

繁荣,我又回来做生意了。

回答by Arun Reddy

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:

在基于 linux 的操作系统上更改 npm 版本还不是一个直接的命令过程。我已完成以下操作以切换回旧版本的 npm。这应该可以使任何版本的 npm 工作。首先安装你要使用的 npm 版本:

sudo npm install -g [email protected]

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

删除 /usr/local/bin/ 中的符号链接

sudo rm /usr/local/bin/npm

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

使用您安装的所需版本的 npm 重新创建符号链接

sudo ln -s /usr/bin/[email protected] /usr/local/bin/npm

回答by jim

By looking at www.npmjs.com/install.shI 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"

export 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_installvariable, then it will install the the version they have marked as latest

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

回答by adnan2nd

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 中,以管理员身份运行您的终端(以防我遇到权限问题)。然后使用特定的节点版本(比如 7.8.0)

nvm use 7.8.0

then update your npm to desired specific version by

然后通过以下方式将您的 npm 更新到所需的特定版本

npm install -g [email protected]

回答by Alvin Smith

  1. find the node and npm version you want to use from here https://nodejs.org/en/download/releases/
  2. nvm use 8.11.4
  3. you already got the npm 5.6(solid and good)
  1. 从这里找到要使用的节点和 npm 版本https://nodejs.org/en/download/releases/
  2. nvm 使用 8.11.4
  3. 您已经获得了 npm 5.6(可靠且良好)

回答by Dimitri Reifschneider

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

怎么样npm i -g npm?您是否也尝试运行此程序?