如何使用 nvm 更改 npm 的版本? [英] How can I change the version of npm using nvm?

查看:72
本文介绍了如何使用 nvm 更改 npm 的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 NVM 为我的节点工作安装最新版本的 nodeJS.它非常适合安装单独的版本并在它们之间切换.它还在每个本地 .../bin 文件夹中安装最新版本的 NPM 以及节点二进制文件.但是,似乎没有任何方法可以切换我正在使用的 NPM 版本(或者至少我无法弄清楚).

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 安装节点时安装的 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.

推荐答案

自从第一次回答这个问题已经好几年了,如 一个较新的答案,现在有一个命令:

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

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

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

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

nvm install-latest-npm: Attempt to upgrade to the latest working npm on 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

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

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

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

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.

我刚刚安装了 node 4.2.2,它与 npm 2.14.7 一起提供,但我想使用 npm 3.所以我做到了:

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

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

简单!

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

And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node.

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

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

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

Thanks @philraj for pointing this out in a comment.

这篇关于如何使用 nvm 更改 npm 的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆