Node Version Manager - Easily switch between Node.js Versions 2023

Amir Mustafa
7 min readSep 26, 2023

--

→ In this article we will be learning NVM i.e. Node Version Manager.

→ If you are working on Node JS projects, our system is basically having a specific version of Node.js (say Node.js 18 or 16 or 14 or 12, etc.)

What is NVM?

→ Node Version Manager is a software that can help us easily switch between multiple Node versions

→ This will save us from while node version uninstall and reinstall with a specific version

Why NVM?

→ When you are working on a project where one project requires a legacy version i.e. eg version x (say Node.js 12) and the second project requires the latest version y (say Node.js 18)

→ So for easily switching between Node.js versions NVM is a savior. An alternate approach is containerizing via docker (this is a different way altogether)

Where do I use NVM?

→ In our project, there are multiple servers running one is the backend server i.e. node.js 12

→ The other one is Azure Functions server i.e. Node JS 12 with Azure function runtime version 3

→ Now Microsoft says to upgrade Function Apps (Serverless Function) to Runtime 4 and Node.js 18 ⚠️

Now think, our backend code will take time to migrate from Node 12 to 18 completely, and this app is working in production.

Solution:

Node Version Manager (NVM): With the help of NVM, we can easily segregate the code for both servers — one will be Node 12 and the other Node 18

OS 1: Installing NVM in Mac/Linux

→ We will refer above official documentation of NVM

STEP 1: Creating .nvrc file

→ Open the terminal in your Mac or Linux OS. We have to create .nvmrc file. If the file exists, then no need to create

→ Let the file be empty

ls -la        // SEARCH if .nvmrc exists

touch .nvmrc // CREATE EMPTY .nvmrc file

STEP 2: Installing nvm

→ Run the below command in your system

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

→ Now when you run the below command, the nvm version will show instead of the nvm command not found

nvm

OS 2: Installing NVM in Windows:

→ In the above link click on the latest installer link

→ Click on nvm-setup.exe downloadable file.

→ We will find screenshots, in the GitHub link itself.

NOTE: Whether we are in Mac, Windows or Linux, the important thing is to restart terminal after installation 💡

Cloning a Node.js Project:

→ We have a basic Node.js, Express.js project that has one feature of Node JS 18 i.e. optional chaining

git clone https://github.com/AmirMustafa/node_nvm.git

→ There is no special feature in this app, except one which we are interested in.

Path: app.js

app.get('/city', (req, res) => {
return res.json({city: person?.address?.city}); // Optional Chaining
});

NOTE: The question mark followed by the dot feature is called Optional Chaining i.e. ?.

// WITHOUT OPTIONAL CHAINING
const x = city && city.person && city.person.address && city.person.address.city

// WITH OPTIONAL CHAINING - Basically checks undefined and prevents from breaking
const y = city: person?.address?.city;

→ This feature came from Node JS 14 onwards

→ We will be using Node JS 12 (Unsupported Optional Chaining) and Node JS 18 (Latest version)

Switching Node.js Versions:

→ Checklist of Node.js versions here:

→ Now it is time for the interesting part. Let us now switch versions

STEP 1: First time install the version

nvm install <node_version> // NO NEED TO WRITE FULL DECIMAL

nvm install 18
nvm install 12
nvm install 8

STEP 2: Switching to a Node.js different version

→ The below command is for the same

nvm use <node_version_number>

eg nvm use 18 // SWITCHING TO NODE JS 18

→ Now we know our project will run in version 18 and break in 12 and 8

→ Let us run the project now

nodemon app

→ Now let us switch to version 12

nvm use 12

→ Let us now run the project again

nodemon app

→ This time project got broken as Node JS 12 does not support Optional Chaining

→ This is expected as the Node JS version is changed

→ Let us revert to Node JS 18

nvm use 18

nodemon app // NODE JS RAN WITHOUT ERROR

→ Hurray, we now know how to switch between Node.js versions 😃

→ There are two more commands we need to know.

Listing installed Node JS versions

→ The below command is for the same

nvm list

or

nvm ls // BOTH COMMANDS WORKS SAME

Deleting installed Node JS versions

→ Now suppose are list has grown a lot and we have installed many Node.js versions.

→ To uninstall the node js version, the below command is used:

nvm uninstall <node_version>
nvm uninstall 8 // LET US ASSUME NODE v8 IS NOT REQUIRED

→ Let us check in list again

Handling Proxy:

→ Some company uses proxy on website for get your proxy URL:
Go to users:

→ It will restrict to use regular nvm operations like npm install, etc.

→ To fix it follow below steps

  1. Go to C:\Users\<Username>
  2. Open .npmrc file
  3. copy proxy
strict-ssl=false
@types:registry=https://registry.npmjs.org
@std:registry=http://registry.npmjs.org
fetch-retry-maxtimeout=6000000
fetch-retry-mintimeout=1000000
legacy-peer-deps=true
registry=https://abc.com
proxy=http://:abc.com:80 // THIS URL IS REQUIRED

Command:

nvm proxy [url of proxy server]

eg. nvm proxy http://:abc.com:80

→ Now your nvm regular command will work

Code:

Closing Thoughts

In this article, we have learned way we can easily switch node js versions inside the same machine.

If we are required to work in multiple Node.js projects where one project needs Node js 12 and other latest version Node js 18. Node Version Manager (NVM) will help us easily switch between versions

No more explicit uninstall and install node js software is required. 🙂

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa
Amir Mustafa

Written by Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/

No responses yet