Home >Web Front-end >JS Tutorial >Installing Multiple Versions of Node.js Using nvm

Installing Multiple Versions of Node.js Using nvm

Jennifer Aniston
Jennifer AnistonOriginal
2025-02-08 13:28:09412browse

Managing Multiple Node.js Versions with NVM

Installing Multiple Versions of Node.js Using nvm

Installing Multiple Versions of Node.js Using nvm

Working on multiple Node.js projects often requires juggling different runtime versions. This article explores how Node Version Manager (NVM) simplifies this process, allowing seamless switching between various Node.js versions without the hassle of manual installations and uninstalls.

Key Advantages of NVM:

  1. Simplified Version Management: NVM streamlines the process of managing multiple Node.js versions, making it easy to switch between projects with varying version requirements.
  2. Cross-Platform Compatibility: While primarily designed for Linux and macOS, Windows users can leverage nvm-windows, a functionally equivalent tool.
  3. Effortless Installation and Usage: NVM offers straightforward installation and commands for installing, listing, and switching Node versions.

Understanding NVM:

NVM (Node Version Manager) is a command-line tool that simplifies the management of multiple Node.js installations. It provides commands to install, switch between, and manage different Node.js versions.

Operating System Support:

NVM directly supports Linux and macOS. For Windows, use nvm-windows (a separate, but similar, project). The core commands outlined below generally work across both NVM and nvm-windows.

Installation:

Windows:

  1. Uninstall any existing Node.js installations.
  2. Delete existing Node.js directories (e.g., C:Program Filesnodejs).
  3. Delete the npm installation directory (e.g., C:Users<user>AppDataRoamingnpm</user>).
  4. Download and run the latest stable nvm-windows installer.

macOS/Linux:

Removing prior Node.js and npm installations is optional but recommended. Numerous online resources guide you through this process.

Install NVM using cURL or wget:

cURL:

<code class="language-bash">curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>

wget:

<code class="language-bash">wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>

(Note: The version number (v0.35.2) might change; check the NVM project page for the latest version.)

After installation, reload or restart your terminal for NVM to take effect.

Using NVM:

Once installed, the nvm command becomes available in your terminal.

Installing Multiple Node.js Versions:

Use nvm install followed by the version number:

<code class="language-bash">curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>

This installs Node.js version 12.14.1. NVM uses semantic versioning (SemVer); nvm install 12.14 installs the latest 12.14.x version. nvm ls-remote (or nvm ls available for nvm-windows) lists available versions.

Installing npm:

NVM installs a compatible npm version with each Node.js installation. Global npm packages are installed per Node.js version in ~/.nvm/versions/node/<version>/lib/node_modules</version>, preventing conflicts.

Switching Between Versions:

Use nvm use followed by the version number:

<code class="language-bash">wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>

This switches to Node.js 13.6.0. nvm use node selects the latest version, and nvm use --lts selects the latest LTS (Long-Term Support) release.

Custom Aliases:

Create custom aliases using nvm alias:

<code class="language-bash">nvm install 12.14.1</code>

Set a default version with nvm alias default <version></version>.

Listing Installed Versions:

Use nvm ls to list installed versions. The currently active version is marked with an arrow. nvm current shows the active version.

Per-Project Node Version Specification:

Create a .nvmrc file in a project directory containing the desired Node.js version. NVM automatically detects and uses this version when you cd into the project directory and run nvm use. (Optional shell configuration may be needed for automatic activation).

Other NVM Commands:

  • nvm run <version> <command></command></version>: Runs a command using a specific Node.js version without switching.
  • nvm exec <version> <command></command></version>: Runs a command in a subshell with a specific Node.js version.
  • nvm which <version></version>: Gets the path to the Node.js executable for a specific version.

Conclusion:

NVM is an invaluable tool for Node.js developers, simplifying version management and saving significant time and effort.

FAQs (briefly addressed within the main text):

  • Supported OS: Linux, macOS, Windows (nvm-windows).
  • Installation: Script execution (details provided).
  • Shell Compatibility: Configurable for various shells.
  • Other Runtimes: Primarily Node.js, but similar tools exist for other languages.
  • Updating NVM: nvm update
  • Multiple Managers: Avoid using multiple simultaneously.
  • Stable/LTS/Dev Versions: All supported.
  • npm Management: Managed alongside Node.js versions.
  • Specific Version Installation/Uninstall: nvm install <version></version>, nvm uninstall <version></version>.
  • Listing Installed Versions: nvm ls
  • Setting Default Version: nvm alias default <version></version>
  • Switching Versions: nvm use <version></version>

Installing Multiple Versions of Node.js Using nvm Installing Multiple Versions of Node.js Using nvm Installing Multiple Versions of Node.js Using nvm

The above is the detailed content of Installing Multiple Versions of Node.js Using nvm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn