Posted on
Software

nvm: Node.js version manager

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Node.js Development with NVM: A Comprehensive Linux Guide

Node.js has become an indispensable tool for developers, particularly for those working in web development. Managing multiple versions of Node.js, however, can be a cumbersome task, especially when transitioning between different projects that may require different versions. This is where Node Version Manager (NVM) comes into play. NVM allows you to install multiple versions of Node.js and switch between them with ease.

In this blog post, we'll explore what NVM is, why you should use it, and provide detailed installation instructions across various Linux distributions using different package managers like apt, dnf, and zypper.

What is NVM?

NVM stands for Node Version Manager. It's a POSIX-compliant bash script to manage multiple active Node.js versions. With NVM, you can install and uninstall any Node.js version that you need to use, without affecting the entire system configuration. This tool is incredibly helpful for developers who need to test applications across different versions of Node.js or switch between projects with different Node.js requirements.

Why Use NVM?

  1. Version Management: Seamlessly switch between Node.js versions depending on your project requirements.
  2. Local Installation: Install different versions of Node.js in isolated environments, thus avoiding global dependencies and conflicts.
  3. Compatibility Testing: Easily test your application across multiple versions of Node.js to ensure compatibility.
  4. No Sudo Required: Install Node.js versions without administrative privileges, keeping your system secure and clean.

Installing NVM on Linux

NVM can be installed using curl or wget. Regardless of your package manager, the initial steps to install NVM are the same.

Step 1: Installing NVM

Open your terminal and run the following command to download and install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Or alternatively, use wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

These commands will download the script and run it. The script clones the NVM repository to ~/.nvm and attempts to add the source lines from the snippet to your correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

Step 2: Source Your Profile

To start using NVM after installation, either close and reopen your terminal or source the profile file where it was installed. You can usually do this with a command like:

source ~/.bashrc

Replace .bashrc with the appropriate file based on your shell and configuration.

Step 3: Verify Installation

To check if NVM was installed correctly, you can use:

nvm --version

This should output the version of NVM installed.

Using NVM to Manage Node.js Versions

Once NVM is installed, installing Node.js becomes straightforward.

Install a specific version of Node.js:

nvm install 14

Switch between installed Node.js versions:

nvm use 12

List installed Node.js versions:

nvm ls

List available Node.js versions to install:

nvm ls-remote

About using apt, dnf, and zypper

For Linux users, especially beginners, it's important to understand that NVM is not typically available through package managers like apt, dnf, or zypper. Instead, it should be installed using curl or wget as described above. This method ensures you are using the latest version of NVM and prevents issues with version compatibility.

In conclusion, using NVM to manage Node.js on Linux platforms offers flexibility and ease for developers. By enabling the use of multiple Node.js versions, NVM ensures that developers can meet their project's needs without interfering with the system's overall Node.js settings. Start leveraging NVM today to boost your Node.js development process!