Managing Multiple Node.js Versions with NVM: A Comprehensive Guide

 



Node.js is a powerful runtime environment that allows you to execute JavaScript code outside of a web browser. As a developer, you may find yourself working on projects that require different Node.js versions due to compatibility or project-specific requirements. This is where Node Version Manager (NVM) comes to the rescue. NVM is a fantastic tool that lets you easily manage multiple Node.js versions on your system. In this blog post, we'll walk you through the installation and usage of NVM.

Prerequisites For Mac

Before you begin, ensure that you have the following prerequisites installed on your system:

  1. Homebrew
To Install Homebrew run below code in terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once above command executed run below

eval "$(homebrew/bin/brew shellenv)" brew update --force --quiet
chmod -R go-w "$(brew --prefix)/share/zsh"

Now home brew is installed and ready to use.

 

Step 1: Installing NVM - Mac

To install NVM, open your terminal and execute one of the following commands

brew install nvm 
now create a .nvm directory using below command
mkdir ~/.nvm 
now create a file directory using below command
vim ~/. zshrc
Add below code in this file and save by pressing 'esc' key then keys : w q and hit enter
export NVM_DIR="$HOME/.nvm"
[ -s"$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ]. && \. "$NVM_DIR/bash_completion" #  This loads nvm bash
Finally run below command
source ~/. zshrc

Step 1: Installing NVM - Windows

Download and install nvm using link below link
NVM for Windows

Step 2: Verifying NVM Installation

You can verify that NVM is installed correctly by running the following command:

nvm --version
This should display the version of NVM you installed.

Step 3: Installing Node.js Versions

Now that NVM is installed, you can easily install different Node.js versions using the following command:

nvm install <node_version>

Replace <node_version> with the specific Node.js version you want to install, such as 14.17.6 or 16.5.0.

Step 4: Using Specific Node.js Versions

NVM allows you to set a specific Node.js version as the default or use it in the current shell session:

To set a default version:

nvm alias default <node_version>

To use a specific version in the current session:

nvm use <node_version>

Step 5: Checking Node.js Version

You can always verify the currently active Node.js version by running:

node -v
Step 6: Switching Between Node.js Versions

One of the greatest benefits of NVM is the ability to switch between installed Node.js versions with ease. To switch to a different version, simply use the nvm use command with the desired version.

With NVM, you have the flexibility to manage and switch between Node.js versions effortlessly, making it a valuable tool for any developer working on various projects.

In conclusion, Node Version Manager (NVM) is an essential tool for managing multiple Node.js versions on your system. Whether you're developing for different projects or ensuring compatibility with specific applications, NVM simplifies the process and empowers you to harness the full potential of Node.js. By following the steps outlined in this guide, you'll be well-equipped to manage your Node.js environments efficiently.

Thanks!

Post a Comment

Previous Post Next Post