乐闻世界logo
搜索文章和话题

How can I change the version of npm using nvm?

5 个月前提问
3 个月前修改
浏览次数151

7个答案

1
2
3
4
5
6
7

当然,nvm(Node Version Manager)是一个用于管理和切换不同Node.js版本的工具。它也允许用户为不同的Node.js版本安装与之兼容的npm版本。以下是如何使用nvm来更改npm版本的步骤:

  1. 安装 nvm: 首先,您需要确保已经安装了nvm。可以通过运行以下命令在Linux或macOS上安装nvm

    sh
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

    或者,如果您使用的是Windows,则可以使用nvm-windows

    sh
    nvm install-latest-npm
  2. 列出可用的 Node.js 版本: 安装好nvm后,您可以查看所有可用的Node.js版本:

    sh
    nvm ls-remote
  3. 安装 Node.js: 使用nvm安装一个特定版本的Node.js。npm会与该版本捆绑安装:

    sh
    nvm install 14.17.0

    这将安装Node.js 14.17.0版本和与之兼容的npm版本。

  4. 切换 Node.js 版本: 安装多个版本的Node.js后,可以使用以下命令在它们之间切换:

    sh
    nvm use 14.17.0

    这将切换到Node.js 14.17.0版本和相应的npm版本。

  5. 升级 npm: 如果你想要为当前使用的Node.js版本升级npm,可以运行:

    sh
    npm install -g npm@latest

    这将为当前选中的Node.js版本安装最新版本的npm

  6. 查看 npm 版本: 可以通过运行以下命令来验证npm的版本:

    sh
    npm --version
  7. 为特定的 Node.js 版本安装特定的 npm 版本: 如果需要为特定的Node.js版本安装一个特定版本的npm,可以先切换到该Node.js版本,然后指定npm版本进行安装:

    sh
    nvm use 14.17.0 npm install -g npm@6.14.13

    这样,您就可以为Node.js 14.17.0安装npm 6.14.13版本。

以上就是使用nvm管理和更改npm版本的基本步骤。这使得在不同的项目之间切换开发环境变得非常简单,因为每个项目可能需要不同的Node.js和npm版本。

2024年6月29日 12:07 回复

As noted in another answer, there is now a command for this:

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

nvm install-latest-npm: Attempt to upgrade to the latest working npm on the current Node.js version.

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

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

For later versions of npm it is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

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

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


In a newer version, npm -g is smart and installs modules into the path above instead of the system global path.

2024年6月29日 12:07 回复

Use

shell
npm install npm@x.x.x -g npm install npm@5.4.0 -g
2024年6月29日 12:07 回复

nvm doesn't handle npm.

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

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

shell
npm install nvm . ~/nvm/nvm.sh

Install Node.js 0.4.x with nvm:

shell
nvm install v0.4.12 nvm use v0.4.12

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

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

This will detect Node.js 0.4.12 and install npm 1.0.106 in your ~/nvm/v0.4.12/lib/node_modules folder and create a symbolic link for nvm:

shell
~/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.

2024年6月29日 12:07 回复

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

2024年6月29日 12:07 回复

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:

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

2024年6月29日 12:07 回复

nvm(Node Version Manager)是一个用于管理和切换不同Node.js版本的工具。它也允许用户为不同的Node.js版本安装与之兼容的 npm版本。以下是如何使用 nvm来更改 npm版本的步骤:

  1. 安装 nvm: 首先,您需要确保已经安装了 nvm。可以通过运行以下命令在Linux或macOS上安装 nvm

    sh
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

    或者,如果您使用的是Windows,则可以使用 nvm-windows

    sh
    nvm install-latest-npm
  2. 列出可用的 Node.js 版本: 安装好 nvm后,您可以查看所有可用的Node.js版本:

    sh
    nvm ls-remote
  3. 安装 Node.js: 使用 nvm安装一个特定版本的Node.js。npm会与该版本捆绑安装:

    sh
    nvm install 14.17.0

    这将安装Node.js 14.17.0版本和与之兼容的 npm版本。

  4. 切换 Node.js 版本: 安装多个版本的Node.js后,可以使用以下命令在它们之间切换:

    sh
    nvm use 14.17.0

    这将切换到Node.js 14.17.0版本和相应的 npm版本。

  5. 升级 npm: 如果你想要为当前使用的Node.js版本升级 npm,可以运行:

    sh
    npm install -g npm@latest

    这将为当前选中的Node.js版本安装最新版本的 npm

  6. 查看 npm 版本: 可以通过运行以下命令来验证 npm的版本:

    sh
    npm --version
  7. 为特定的 Node.js 版本安装特定的 npm 版本: 如果需要为特定的Node.js版本安装一个特定版本的 npm,可以先切换到该Node.js版本,然后指定 npm版本进行安装:

    sh
    nvm use 14.17.0 npm install -g npm@6.14.13

    这样,您就可以为Node.js 14.17.0安装npm 6.14.13版本。

以上就是使用 nvm管理和更改 npm版本的基本步骤。这使得在不同的项目之间切换开发环境变得非常简单,因为每个项目可能需要不同的Node.js和 npm版本。

2024年6月29日 12:07 回复

你的答案