- Install nvm: First, ensure that
nvmis installed. To installnvmon Linux or macOS, run the following command:
shcurl -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:
shnvm install-latest-npm
- List Available Node.js Versions: Once
nvmis installed, you can view all available Node.js versions:
shnvm ls-remote
- Install Node.js: Use
nvmto install a specific version of Node.js.npmwill be bundled with this version:
shnvm install 14.17.0
This will install Node.js 14.17.0 and the compatible npm version.
- Switch Node.js Version: After installing multiple versions of Node.js, you can switch between them using the following command:
shnvm use 14.17.0
This will switch to Node.js 14.17.0 and the corresponding npm version.
- Upgrade npm: If you want to upgrade
npmfor the currently used Node.js version, run:
shnpm install -g npm@latest
This will install the latest version of npm for the currently selected Node.js version.
- Check npm Version: You can verify the
npmversion by running the following command:
shnpm --version
- Install Specific npm Version for a Node.js Version: If you need to install a specific version of
npmfor a specific Node.js version, first switch to that Node.js version, then install the specifiednpmversion:
shnvm 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 回复