How To Have Multiple Node Versions On Your Dev Machine 🤔 ?

Sometimes you want to have different versions of Node.js on your dev machine. For example, on my MacBook, the default node version was 10.22. I wanted to try the ES6 modules feature, which is supported since node v13.2 without requiring the use of the experimental-module flag. When I checked the Node.js website for the latest Node.js versions, I found two versions which I was interested in: v 14.15 & v15.3.

https://nodejs.org/en/

Node Version Manager helps you install & use different node versions. Let’s quickly walk through the steps of how we can install and use these two new node versions.

Install NVM

Run the below command(s) in your terminal:

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

Download the desired node version(s)

nvm install 14.15
nvm install 15.3

Use a node version

Let’s try to use v14.15. Here’s how you can do that:

nvm use 14.15# verify
node -v

Similarly if we want to switch to a different node version, we can use do that by:

nvm use 15.3# Verify
node -v

You will notice that whichever node version we select using nvm, it’s not persistent across terminal sessions, i.e. that node version resets to default. version after we restart the terminal. You can solve this problem by the command nvm alias. Suppose we want to use v14.15 as the default node version. You can do so by using the command:

nvm alias default 14.15

And voila! Every-time you restart your terminal, you will get node v14.15.

ES6 Modules

Finally, let my try out the ES6 modules!

mkdir try-esm; cd try-esm; yarn init -y;

In order to use ES6 modules, add the following line in your package.json:

"type": "module"

Let’s try to write some code! The contents of sum.js are:

// sum.jsexport default function add(numOne, numTwo) {
return numOne + numTwo;
}

Contents of index.js are:

// index.jsimport add from './sum.js';
console.log(add(1, 2));

The moment of truth 🥁:

node index.js

Success 🎉 Hope this blog post has been helpful 👍

Nom Nom

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store