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

查看:1880
本文介绍了如何使用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 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.

我刚安装了npm 2.14.7附带的节点4.2.2,但是我想使用npm3.所以我做到了:

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.

在最新版本中,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天全站免登陆