What is nvm (Node Version Manager
nvm (Node Version Manager) is a tool that allows you to manage multiple versions of Node.js on a single machine. This is particularly useful when working on different projects that require different versions of Node.js or when you need to test your applications against various Node.js versions. Here’s a detailed overview of nvm:
What is nvm?
nvm is a command-line tool that enables you to install, switch between, and manage different versions of Node.js. It also allows you to manage npm (Node Package Manager) versions that are bundled with each Node.js version. By using nvm, you can easily switch between different Node.js environments without affecting your global setup or other projects.
Key Features of nvm
Multiple Versions:
- Allows you to install and manage multiple versions of Node.js on your system.
Version Switching:
- Easily switch between different versions of Node.js as needed for different projects.
Version Isolation:
- Each Node.js version managed by nvm is isolated from others, which helps avoid conflicts between projects.
Global npm Packages:
- Each Node.js version has its own set of global npm packages, avoiding conflicts and ensuring compatibility.
Easy Installation:
- Provides a straightforward method for installing, updating, and managing Node.js versions.
Installing nvm
For Unix-based Systems (Linux/macOS)
Download and Install nvm:
- Open your terminal and run:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
- This command downloads and runs the installation script for nvm.
- Open your terminal and run:
Load nvm:
- After installation, you need to add nvm to your shell’s environment. Add the following lines to your shell profile file (
~/.bashrc
,~/.bash_profile
, or~/.zshrc
):export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
- Reload your shell configuration by running:
source ~/.bashrc
- or for zsh:
source ~/.zshrc
- After installation, you need to add nvm to your shell’s environment. Add the following lines to your shell profile file (
Verify Installation:
- Check if nvm is installed correctly:
nvm --version
- Check if nvm is installed correctly:
For Windows
- nvm-windows is a Windows-compatible version of nvm. You can find it here.
Download the Installer:
- Go to the nvm-windows releases page.
- Download the latest
.exe
installer.
Run the Installer:
- Double-click the downloaded
.exe
file and follow the setup instructions.
- Double-click the downloaded
Verify Installation:
- Open Command Prompt or PowerShell and run:
nvm version
- Open Command Prompt or PowerShell and run:
Using nvm
1. Installing Node.js Versions
List Available Versions:
- To see a list of available Node.js versions:
nvm ls-remote
- To see a list of available Node.js versions:
Install a Specific Version:
- Install a specific version of Node.js:
nvm install <version>
- Example:
nvm install 18.17.0
- Install a specific version of Node.js:
2. Switching Between Versions
List Installed Versions:
- View all installed Node.js versions:
nvm ls
- View all installed Node.js versions:
Use a Specific Version:
- Switch to a specific Node.js version:
nvm use <version>
- Example:
nvm use 18.17.0
- Switch to a specific Node.js version:
3. Setting Default Version
- Set Default Version:
- Set a default Node.js version that will be used in new shell sessions:
nvm alias default <version>
- Example:
nvm alias default 18.17.0
- Set a default Node.js version that will be used in new shell sessions:
4. Uninstalling Versions
- Uninstall a Specific Version:
- Remove a specific Node.js version:
nvm uninstall <version>
- Example:
nvm uninstall 18.17.0
- Remove a specific Node.js version:
Benefits of Using nvm
- Flexibility: Easily switch between different Node.js versions for different projects or testing environments.
- Isolation: Avoid conflicts between different versions and global npm packages.
- Convenience: Simplify the management of Node.js versions and streamline development workflows.
Summary
- nvm is a version manager for Node.js that helps you install, manage, and switch between different Node.js versions.
- Installation: Use a script for Unix-based systems or an installer for Windows.
- Usage: Install, use, set defaults, and uninstall Node.js versions as needed.
- Benefits: Provides flexibility, isolation, and convenience for managing Node.js versions and packages.