Categories
Hard Skills

Install NVM instead of NodeJS directly

What is NVM

Many times, when I am working on a new NodeJS project, I have colleagues who have never heard of NVM or Node Version Manager.

As complicated as this 3-letter acronym sounds, it is a very simple tool to use, but it has a great impact on the development experience.

Why to use NVM

Nowadays companies use Micro Service Architectures, meaning the working software will be written in many small software projects, that will communicate over some networking infrastructure. As multiple teams will work on parts of the system, these projects most likely will use a different version of NodeJS.

It can even happen that some required NPM packages are only supported in specific version of NodeJS and you need to separate you logic behind a legacy NodeJS version, that conflicts with your main project you mostly working on.

If you install NodeJs directly on your machine, it means at any given time, you will have the same version of NodeJS and when you fire up a project with a different version, you will have to uninstall what you have, and install the other version. It can get cumbersome very quickly.

But what do you do, if you need to run multiple projects locally at the same time, needing different version of NodeJS? Without NVM, you simply don’t.

An alternate solution is to use Docker, or to run those microservices in the cloud, but it heavily slows down development, or just simply, sometimes, scenarios do not allow it, for any reason.

NVM solves the problem

If you use NVM, you have multiple options to ease your work flow.

The simplest is to say:

nvm use v22

or any of the version you require.

You can check the available versions with

nvm ls

and also, you can install any version with

nvm i v<your version number here>

like

nvm i v22

My preferred approach is to make a “.nvmrc” file and add it to your source control. It’s content should only contain the version number of NodeJS, like

v22

That way everyone who checks out your code can simply type

nvm use

and if you have the .nvmrc file, it will detect the version, and use it for you.

If you have multiple projects, you just have to open them in a separate terminal since nvm will use NodeJS per terminal open’s session, so you can have each terminal window using a different version of NodeJS if you’d like.

Installation of NVM

NVM originally is a Unix/Linux tool, but it has projects that serves the same purpose on Windows as well.

Whenever I have the chance I either go with native Unix/Linux setup or WSL on windows.

Simply because developing for NodeJs, as of my experience is easier on Linux than Windows.

There are many great websites on how to set up NVM, but I’ll include the happy path steps here.

As a rool of thumb, regardless what OS you are using, make sure you do not have NodeJS installed natively on your computer. If you have, uninstall it.

Install NVM on Ubuntu / WSL

Thanks for these steps for: Geeks for Geeks

Step 1: Update Linux

sudo apt-get update

Step 2: Install curl

sudo apt install curl

Step 3: Using curl download NVM installer script from GitHub (you might want to Google it, if there are newer version of the installer script)

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

Step 4: Either restart your terminal, or load the config directly with this command

source ~/.bashrc

Install NVM on MacOS

To install NVM on MacOS I prefer to use the brew package manager and their instructions. On MacOs I saw people struggle a bit more, because you need to configure .zshrc manually, but if you follow the instractions here, it works.

Step 1: Install brew following this link

Step 2: Follow the instractions here

Install NVM on Windows

The NVM project for Windows is a different project compared to its Linux counterpart, simply because of OS differences.

The project GitHub page I used before is: https://github.com/coreybutler/nvm-windows

You can simply download the installer and run it as for any other Windows application, but you have the option to install it through command line if you prefer that.

Note: the GitHub page mentions that the original developers are working on a new alternative project, but I do not see any installer for their new project, so keep an eye on the updates here: https://github.com/coreybutler/nvm-windows/wiki/Runtime

By Botond Bertalan

I love programming and architecting code that solves real business problems and gives value for the end-user.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.