The Ultimate Guide to Node.js Version Managers: NVM, NVS, fnm, Volta, and asdf | Part 1
Introduction
Hey! If you ever developed with Node.js, you understand that quite often for projects, there’s a need to use different versions. Probably one project would work on version 10, another on version 14, and a new one requires the latest, say 20. Along with new features on each new release, a set of new challenges emerge. These are compatibility with libraries and frameworks, tests of new functionality, and stability for existing projects.
I faced this problem myself when I was working on several projects at one time. What seemed to be a very simple task — installation of Node.js — turned into chaos when each project required its version. In this article, I will tell you how I solved this problem using Node.js version management tools like NVM, NVS, fnm, Volta, and asdf. I’ll be describing how they work, listing pros and cons, and giving you my personal experience in order to help you choose the best node version manager for your needs.
Why is Version Management Important for Developers?
Compatibility with Libraries and Frameworks
Node.js itself is rapidly developing, and so is its tooling ecosystem. New libraries, frameworks, and versions require a great deal of flexibility in using different Node.js versions. Some view frameworks may only be compatible with particular Node.js LTS versions that one would have to switch to according to the project being developed. Switching between different Node.js versions will help avoid compatibility problems and keep the code running smoothly.
Consider that you work on some old project, which depends on a specific version of the library. You at the same time run some new project depending on the latest version of Node.js since it uses the features available only within the recent version. New versions of Node.js may include functions incompatible with versions of these libraries, and that will lead to application performance errors or instability.
One day, I got into such a predicament where I needed to install different versions of Node.js manually, work with that, then reinstall another version, and so on and so forth. Believe me, that was a nightmare. And then, it dawned on my mind that without a node version manager utility tool, I couldn’t do anything.
New Feature Testing
Software development is all about continuous testing and implementation of new features. Each new version of Node.js exposes developers to additional language and platform capabilities, such as enhanced asynchronous programming support, improvements in the module system, and new APIs. Such features would then be tested on real projects to ascertain how effective they were and whether to implement them into the main application.
But what if your current project is running stable under an older version of Node.js, and this might get broken after upgrading Node.js?
That often meant checking new features in the master branch, or a copy of the project using the new Node.js version. Luckily, tooling for version management allowed me to switch between different versions with no brokenness in the master branch.
Ensuring Stability and Security
Stability and security are the major factors for any project. In older versions of Node.js, some bugs may be held, which get fixed with new releases. Upgrading to a recent version is pretty risky if an application depends on the older libraries that support new platform version upgrades.
Versioning Node.js allows you to safely upgrade the platform version while retaining the possibility to roll back in the case of problems, thus helping developers to keep their application stable and safe from vulnerabilities.
Ways to Manage Node.js Versions
Standard Installation
If you are a newcomer to Node.js, you’ve probably downloaded it from its official website and installed it. That is the most straightforward way that is great when you need only one version of Node.js. You download the installer, follow the instructions, and voilà — Node.js is on your computer.
But imagine that you work with several projects and all of them require some specific versions of Node.js. For example, you have some old project on Node.js 10 and some new one on Node.js 20. Constant reinstalling of Node.js is too time-consuming and just inconvenient.
Node.js Version Management Tools
There are plenty of tools for managing Node.js versions. In this article, I’m going to discuss five popular options: NVM (Node Version Manager), NVS (Node Version Switcher), fnm, or Fast Node Manager, Volta, and asdf. All of these come with their ways and features to manage the versions, which might be applicable for various tasks and teams.
Those version managers will automate the management process, manage the consistency of the versions, and avoid compatibility features, advantages, and disadvantages to enable you to select the right tool for your needs.
Integration with CI/CD Tools
When you start working in a team, version management becomes way more important. Each developer might own their version of Node.js, which may get very problematic at different development and deployment stages as different bugs could arise. In large projects and teams where automation plays a huge role, such Node.js version management tools can be integrated into the CI/CD processes.
Tools like NVM can be integrated into CI/CD processes, allowing each build to use the required version of Node.js without manual intervention, ensuring that every team member uses the correct Node.js version for their tasks. This helps maintain stability and consistency across different environments, such as development environment, testing, and production.
Overview of Node.js Version Management Tools
Now, the different tools managing different Node.js versions have their pros and cons, and I’ll try to explain what situation each tool fits best.
NVM (Node Version Manager)
NVM is short for Node Version Manager. It is one of the oldest and still very popular managers of Node.js versions. NVM was created by Tim Caswell in 2010 and is still actively maintained.
NVM downloads each Node.js version into its own, self-contained directory at ~/.nvm/versions/node/.
When you’re switching between versions by using nvm use
, it updates your $PATH environment variable to point to the appropriate directory.
How to Install and Use Installation on macOS and Linux:
To install NVM on macOS and Linux, follow these steps:
Open the terminal and enter the following:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
This will download and run the install script for NVM from the official NVM GitHub repository, and the NVM will be installed on your system. Once it has been installed you can verify that NVM has been installed using the command:nvm — version
If everything went smooth, you should see the nvm version
Installation for Windows:
Download the
nvm-windows
installer from its GitHub repository.Run the installer and follow the on-screen instructions, making sure to choose the correct installation path.
You should see the version of NVM if it has installed correctly.
During the installation process, it will automatically set up NVM configurations in your profile. If you are using zsh, that would be ~/.zshrc
or if you’re using bash, that would be ~/.bashrc
, ~/.bash_profile
, or some other profile.
If that does not happen automatically, add the NVM configuration to the profile file yourself.
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"
Using NVM:
List available Node.js versions:
nvm ls-remote
Install the latest Node.js version:
nvm install node
Install a specific version:
nvm install 14.17.0
ornvm install 14
Switch between versions:
nvm use 14.17.0
ornvm use 14
Set default version:
nvm alias default 18
View installed versions:
nvm ls
Version from
.nvmrc
: If your project directory contains a .nvmrc file specifying the Node.js version required-e.g., 16.13.0 or 20-you can automatically switch to that version with nvm use.
Advantages of NVM:
Installation and Usage: Done within a few minutes, and thereafter, it’s quite easy to handle various Node.js versions.
Flexibility: It allows one to run or install many Node.js versions on one device, not conflicting.
Compatibility: NVM works on most Unix-like systems and runs on macOS or Linux. For Windows, there’s a port called nvm-windows.
Support for
.nvmrc
files: This file allows you to automatically switch to the desired Node.js version when entering a project directory.Community and documentation: A substantial user base with good documentation makes NVM a trustworthy choice.
Disadvantages of NVM:
Performance: Sometimes, NVM can be slower than other tools, especially when frequently switching between versions.
Manual management: For some developers, manually switching versions may seem inconvenient, especially if automation is required.
Inconvenience on Windows: If you are working on Windows, using NVM through WSL can be inconvenient.
Subscribe to my newsletter
Read articles from Erik Harutyunyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by