Node.js is an essential tool for developers, especially those working in JavaScript or building server-side applications. One challenge that developers face is managing different versions of Node.js for various projects. 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, we’ll guide you through the process of installing NVM and using it to manage your Node.js versions.

What is NVM?

Node Version Manager (NVM) is a version manager for Node.js, designed to simplify the process of managing multiple active Node.js versions. It is especially useful in development environments where different projects may require different Node.js versions.

Installing NVM

Installing NVM on Linux and macOS

To get started with NVM, you need to install it on your machine. You can install NVM on a UNIX-like operating system such as Linux or macOS. A variant called NVM for Windows is available for Windows users, which works similarly.

Option 1: Using Homebrew

For macOS users, and those on Linux who prefer using Homebrew, installing NVM can be very straightforward:

  1. Open your terminal.
  2. Install NVM using Homebrew:
   brew install nvm
  1. Set up the necessary directory and add it to your profile:

Add these lines to your ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc file:

export NVM_DIR="$HOME/.nvm" [ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh" 
# This loads nvm [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
  1. Source your profile to add NVM to your session:
   source ~/.bashrc
   # Or replace .bashrc with your specific shell profile file


Option 2: Using cURL or Wget


This method works on both Linux and macOS and involves running a simple install script:

1. Open your terminal.
2. Run the install script using cURL or Wget:

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

This script clones the NVM repository to `~/.nvm` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`).

3. Source your profile to add NVM to your session:

source ~/.bashrc
# Or replace .bashrc with your specific shell profile file

With either option, once you have NVM installed, you can begin installing and managing different versions of Node.js as needed.

For Windows:

  • Visit the NVM for Windows repository and download the latest installer. Execute the installer and follow the on-screen instructions.

Installing Node.js Using NVM

Once NVM is installed, you can install Node.js. Here’s how to do it:

  1. List all available Node.js versions:
   nvm list-remote
  1. Install a specific version of Node.js:
   nvm install 16.13.0
   # You can replace "16.13.0" with any desired version
  1. Alternatively, install the latest LTS version:
   nvm install --lts

Switching Between Node.js Versions

With multiple Node.js versions installed, switching between them is straightforward:

  1. List installed Node.js versions:
   nvm list
  1. Switch to a different version:
   nvm use 20.12.2
   # Replace "20.12.2" with your targeted version

Setting a Default Node.js Version

To avoid setting the Node.js version in every new terminal session, set a default version:

nvm alias default 20.12.2
# Replace "20.12.2" with your preferred default version

Conclusion

NVM is a powerful tool that simplifies the management of multiple Node.js versions, making it ideal for both development and production environments. By following these steps, you can install NVM, manage different Node.js versions, and switch between them as needed for your projects. Happy coding!

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *