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

  1. Multiple Versions:

    • Allows you to install and manage multiple versions of Node.js on your system.
  2. Version Switching:

    • Easily switch between different versions of Node.js as needed for different projects.
  3. Version Isolation:

    • Each Node.js version managed by nvm is isolated from others, which helps avoid conflicts between projects.
  4. Global npm Packages:

    • Each Node.js version has its own set of global npm packages, avoiding conflicts and ensuring compatibility.
  5. Easy Installation:

    • Provides a straightforward method for installing, updating, and managing Node.js versions.

Installing nvm

For Unix-based Systems (Linux/macOS)

  1. 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.
  2. 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
  3. Verify Installation:

    • Check if nvm is installed correctly:
      nvm --version

For Windows

  • nvm-windows is a Windows-compatible version of nvm. You can find it here.
  1. Download the Installer:

  2. Run the Installer:

    • Double-click the downloaded .exe file and follow the setup instructions.
  3. Verify Installation:

    • Open Command Prompt or PowerShell and run:
      nvm version

Using nvm

1. Installing Node.js Versions

  • List Available Versions:

    • To see a list of available Node.js versions:
      nvm ls-remote
  • Install a Specific Version:

    • Install a specific version of Node.js:
      nvm install <version>
    • Example:
      nvm install 18.17.0

2. Switching Between Versions

  • List Installed Versions:

    • View all installed Node.js versions:
      nvm ls
  • Use a Specific Version:

    • Switch to a specific Node.js version:
      nvm use <version>
    • Example:
      nvm use 18.17.0

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

4. Uninstalling Versions

  • Uninstall a Specific Version:
    • Remove a specific Node.js version:
      nvm uninstall <version>
    • Example:
      nvm uninstall 18.17.0

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.