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

How can I change the version of npm using nvm?

1个答案

1
  1. Install nvm: First, ensure that nvm is installed. To install nvm on Linux or macOS, run the following command:
sh
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Alternatively, if you are using Windows, you can use nvm-windows:

sh
nvm install-latest-npm
  1. List Available Node.js Versions: Once nvm is installed, you can view all available Node.js versions:
sh
nvm ls-remote
  1. Install Node.js: Use nvm to install a specific version of Node.js. npm will be bundled with this version:
sh
nvm install 14.17.0

This will install Node.js 14.17.0 and the compatible npm version.

  1. Switch Node.js Version: After installing multiple versions of Node.js, you can switch between them using the following command:
sh
nvm use 14.17.0

This will switch to Node.js 14.17.0 and the corresponding npm version.

  1. Upgrade npm: If you want to upgrade npm for the currently used Node.js version, run:
sh
npm install -g npm@latest

This will install the latest version of npm for the currently selected Node.js version.

  1. Check npm Version: You can verify the npm version by running the following command:
sh
npm --version
  1. Install Specific npm Version for a Node.js Version: If you need to install a specific version of npm for a specific Node.js version, first switch to that Node.js version, then install the specified npm version:
sh
nvm use 14.17.0 npm install -g npm@6.14.13

This will install npm 6.14.13 for Node.js 14.17.0.

The following are the basic steps to manage and change npm versions using nvm. This simplifies switching development environments across different projects, as each project may require different Node.js and npm versions.

2024年6月29日 12:07 回复

你的答案