How to switch Node.js versions with NVM
switching nodeNs vesions with NVM and DeadSimpleChat

How to switch Node.js versions with NVM

Dead Simple Chat Team

In this article we are going to learn about how to switch between various nvm versions

  • What is NVM?
  • How to install NVM the step by step Guide
  • Verifying NVM is properly installed
  • Installing Specific versions of Node JS
  • Setting a default version of node Js with NVM
  • How to check for Node versions available to install
  • Switching between Node versions
  • Automatic version switching with .nvmrc
  • Strategies for ensuring consistency across development environments
  • conclusion

What is NVM?

Node version manager also called as NVM is a software utility that allows developers to switch between different versions of node

Some of the important features of NVM

  1. Multiple version management
  2. Easy switching
  3. Version specific global packages
  4. No need for sudo
  5. compatibility testing
  6. Legacy support

How to install NVM? Step by Step guide (Mac, Linux and Windows)

In this section we are going to learn how to install nvm in a step by step manner. We are also going to cover different operating systems such as Mac, linux and windows

So, no matter what operating system you are using, we have got you covered

Installing NVM on macOS and Linux

The most common method of install nvm on mac or linux is using the curl script or wget. As both mac as well as linux use the similar terminal program bash or similar, method of install is also the same

If you are looking for a JavaScript Chat SDK and API, you can consider DeadSimpleChat SDK

On linux if you do not have the curl installed, first install the curl software like

install curl

sudo apt install curl

using curl

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

using wget

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

updating the profile

you need to add the nvm script to your shell profile for it to work. The installation script usually automatically adds the nvm to your shell profile, if for some reason it doesn't then

Use the below script to add it to your .bashrc , .profile or .zshrc

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Verifying installation

nvm --version

Installing nvm on windows

generally windows users use WSL for development and windows is also encouraging this. But if for any reason you need to install nvm on your windows machine

You can install the nvm-windows library

Installing specific versions NodeJs

You can install latest version of node js or any specific version of node using nvm

to install a specific version of node use

nvm install 14.16.0 # write the specific version that you want to install

installing the latest version using nvm

nvm install node

You can check whether the node version has changed properly you can type the below command

node --version # outputs the desired node js version

Setting the default node js version with nvm

you can also set a node js version as default on your machine. It can be the latest version or it can be any version that you desire

you can do this by

nvm alias default 18.4.0

then you can verify that the default node version is set by

node --version

How to check for Node versions available to install

It is quite easy to check for nodejs versions that are available to install. Simply, open your terminal and type the following command

nvm ls-remote

It gives you a whole list of nodejs to choose from like this

Node js versions you can install with nvm
  • the list is ordered from oldest to newest versions
  • each line is a different version of node
  • LTS versions are highlighted with the word LTS written besides them
  • If you are interested in the LTS version of a specific version you can type nvm ls-remote --lts

to install a specific version just type

npm install 20.7.0

Switching between different versions of node

It is quite easy to switch between different versions of node.

  1. List all the versions available to switch between with nvm ls or you can install a version from the remote repository and then switch to that version using nvm ls-remote
  2. Then type the below command to switch to a particular version with
nvm use 18.18.2

3. You can check whether you have successfully switched to the selected version with the node -v command

Automating Version Switching with .nvmrc:

How to automate the process of switching Node.js versions in a project environment.

Step 1: Creating an .nvmrc file

  • navigate to your projects root directory then
cd /path/root_directory
  • create the .nvmrc file

you can create a new file like this

touch .nvmrc
  • Specify the node js version that you want to use in that specific project
18.18.0

and this tells nvm to use the specificed version of node when running your project

Automating the switching the process

Here are the steps to automate the switching process.

You want to automatically run the nvm run peocess whenever you navigate to the project directory

for this you need to add a script to your shells profile

  1. Edit the shell profile
  • for bash go to ./bashrc or ./bash_profile
  • for zsh users edit the ~/.zshrc

2. Add the following script

include the following script in your profile file

autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

then reload the profile with the following code

source ~/.zshrc  # Or the appropriate file for your shell

and that's it now you know how to install and use different versions on Nodejs with NVM

DeadSimpleChat

Need JavaScript Chat API for your website or app

DeadSimpleChat is an Chat API provider

  • Add Scalable Chat to your app in minutes
  • 10 Million Online Concurrent users
  • 99.999% Uptime
  • Moderation features
  • 1-1 Chat
  • Group Chat
  • Fully Customizable
  • Chat API and SDK
  • Pre-Built Chat

Need TURN server, we recommend going with Metered TURN servers starting at 0.04 USD / GB

If you need a free version of the turn server we also have the OpenRelayProject.org a free TURN server.