Editor’s note: This article was last updated by David Omotayo on 25 April 2024 to include information about automating version switching, including why and how to implement it, as well as to offer some alternative Node version managing tools to consider.
Sometimes it seems like new versions of Node.js are released almost weekly — minor versions every few weeks, and major versions every few months. If you are a developer who needs to switch between different applications and projects regularly, you may find you need to run different versions of Node.
Luckily, there are several ways to install multiple versions and switch as needed. This article will discuss and compare two popular Node version managers: NVM for Windows and the n
Node version manager for Linux/Mac.
N.B., Different nvm implementations exist for Windows and Linux/Mac; however, the
n
npm package is only supported on Linux/Mac.
For comparison purposes, let’s pretend that you are working on two applications. The first application is an Angular 12 app running on Node 14.19.0, while the second application is an Angular 11 app running on Node 12.11.1. Here is what you need to accomplish:
You are actually going to need three versions of Node to complete your tasks because the Angular 13 upgrade requires you to upgrade Application 2 to Node 14.15.0 or greater; we’ll use 16.18.1.
Technically, two completely separate NVM packages offer similar capabilities on different operating systems but are maintained independently of each other:
This article focuses on nvm-windows.
Installation is as simple as downloading the NVM for Windows installer from the latest release on GitHub. Download and extract nvm-setup.zip
and double-click on the executable to install.
The installer will place nvm
in an appropriate folder on your machine and update your system environment variables so that nvm
and future installations of node
are available on the command line. Further detailed installation instructions are available on GitHub.
Tip: If you prefer to install to your own folder, download
nvm-noinstall.zip
and extract it wherever you would like. Run the includedinstall.cmd
to set up necessary system environment variables.
Once installation is complete, open a command window and confirm that nvm
is available:
D:\>nvm version 1.1.10
nvm-windows allows for the switching of Node versions back and forth. So far, we have the desired versions installed. Let’s see how we can switch between Node versions 18.21.1, 16.18.1, and 14.19.0 using nvm-windows:
D:\>nvm install 18.21.1 ... D:\>nvm install 16.18.1 ... D:\>nvm install 14.19.0 ... D:\>nvm list 18.12.1 * 16.18.1 (Currently using 64-bit executable) 14.19.0 D:\>nvm use 18.12.1 Now using node v18.12.1 (64-bit) D:\>nvm list * 18.12.1 (Currently using 64-bit executable) 16.18.1 14.19.0 D:\>nvm use 14.19.0 Now using node v14.19.0 (64-bit) D:\>nvm list 18.12.1 16.18.1 * 14.19.0 (Currently using 64-bit executable)
nvm install
installed the required Node version (a 64-bit version by default as the current system’s architecture is 64-bit)nvm use
allows us to select which installed Node version to usenvm list
lists all the installed Node versions with an asterisk (*) next to the active versionNow that we know how to switch to a specific Node version, we can begin working on our sample applications.
If you recall, you need to work on two different applications with three different versions of Node to complete all of your tasks. Start by running Application 1 first. Some command output has been truncated (...
) to save space:
D:\>nvm list available | CURRENT | LTS | OLD STABLE | OLD UNSTABLE | |--------------|--------------|--------------|--------------| | 19.5.0 | 18.13.0 | 0.12.18 | 0.11.16 | | 19.4.0 | 18.12.1 | 0.12.17 | 0.11.15 | ... D:\>nvm install 14.19.0 Downloading node.js version 14.19.0 (64-bit)... Complete Creating C:\Users\Brian\Downloads\nvm-noinstall\temp Downloading npm version 6.14.16... Complete Installing npm v6.14.16... Installation complete. If you want to use this version, type nvm use 14.19.0 D:\>nvm use 14.19.0 D:\>nvm current v14.19.0 D:\>nvm list * 14.19.0 (Currently using 64-bit executable) D:\>node -v v14.19.0 D:\>cd application1 D:\application1>npm install ... D:\application1>npm start > [email protected] start D:\application1 > ng serve ** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** ...
Here are some of nvm’s key capabilities that you just took advantage of to run the application:
nvm list available
provided a convenient partial list of Node versions available to be installednvm current
displays the current Node version in useOnce Node is installed and activated, then it is business as usual. You can follow whatever Node/npm workflow your application requires.
Tip: Your Node versions are completely isolated from each other. For example, if you install a package globally on one version of Node, that package won’t be available on other Node versions.
At this point, you have fixed bug X in Application 1, and you’re now ready to tackle upgrading Application 2 to Angular 8:
D:\nvm install 12.11.1 ... D:>nvm use 12.11.1 Now using node v12.11.1 (64-bit) D:>cd application2 D:\application2>npm install ... D:\application2>npm start ... D:\application2>nvm install ... D:\application2>nvm use 16.18.1 Now using node v16.18.1 (64-bit) D:\application2>npm i -g @angular/cli@13 ... D:\application2>ng update @angular/cli @angular/core ... D:\application2>npm install ... D:\application2>npm start ...
With the help of NVM (and Angular CLI), you made quick work of the upgrade with a few commands:
nvm install
and nvm use
installed and activated v12.11.1 so you could verify that the application worked as expected before the upgradenvm install
and nvm use
installed and activated v14.19.0 in preparation for the upgrade@angular/cli
package to get access to the ng update
command that automatically upgrades Angular applicationsnpm install
and npm start
to test the newly upgraded applicationn
The n
Node version manager is an open source CLI tool that allows you to effortlessly install, switch between, and simultaneously manage multiple Node.js versions on your computer. n
supports a variety of popular operating systems, including macOS, Linux, and Windows Subsystems for Linux (WSL).
Compared to NVM, n
offers a simpler installation process and easier control over installing multiple Node.js versions, switching between versions, and listing installed versions.
Aside from its simplicity, n
also provides additional project compatibility and improved efficiency benefits. These benefits ensure that projects that require specific Node.js versions run correctly and streamline development workflows by allowing you to switch between versions quickly.
n
If you have a version of Node and npm installed already, you can install n
just like any other npm package as follows:
npm install -g n
If you don’t already have a version of Node or npm installed, you can install n
with a Bash script from GitHub. Here is what it looks like:
Tip: You must have Git installed to install
n
with the Bash script.
~$ curl -L https://bit.ly/n-install | bash ... === n successfully installed. ... Run `n -h` for help. To update n later, run `n-update`. To uninstall, run `n-uninstall`. IMPORTANT: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/brian/.bashrc` before using n and Node.js. === ~$ . /home/brian/.bashrc ~$ n node/16.18.1
n
is installed by downloading and running the n-install
script from GitHub. After installation, running n
demonstrates that a version of Node is installed by default.
n
Node version managerApplication 1 requires Node 14.19.0, so you need to install that first, then run the app:
~$ n 14.19.0 install : node-v14.19.0 mkdir : /home/brian/n/n/versions/node/14.19.0 fetch : https://nodejs.org/dist/v14.19.0/node-v14.19.0-linux-x64.tar.gz ####################################################################################################################################### 100.0% installed : v14.19.0 ~$ node -v v14.19.0 ~$ cd application1 ~/application1$ npm install ... ~/application1$ npm start > [email protected] start ~/application1 > ng serve ** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** ...
The n
command for installing and activating a version of Node is simple: n 14.19.0
. You could also use n latest
for the latest version of Node or n lts
for the latest LTS version of Node. If the version of Node is already installed, then n
will simply switch to that version.
After installing Node, the application can run as usual.
N.B., Similar to nvm, Node versions are completely isolated from each other. For example, globally installed packages are not shared between Node versions.
n
Next up, you need to get Application 2 running and proceed with the Angular 8 upgrade:
$ n 12.11.1 ... $ cd application2 ~/application2$ npm install ... ~/application2$ npm start ... ~/application2$ n 16.18.1 ... ~/application2$ npm i -g @angular/cli@13 ... ~/application2$ ng update @angular/cli @angular/core ... ~/application2$ npm install ... ~/application2$ npm start ...
Node 12.11.1 was installed to make sure Application 2 was working prior to the upgrade. Then, Node 16.18.1 is installed as required by Angular 13. The Angular CLI is installed globally, and the application is updated with ng update
.
Finally, the application is started to test after the upgrade. You might have noticed that n
makes it slightly quicker to install and switch to new versions of Node with a single n <version>
command.
n
offers the ability to invoke a specific Node binary directly without having to explicitly switch to that Node version. NVM does not have a similar capability:
~$ echo "console.log('Node version: ' + process.version)" > index.js ~$ node -v v16.18.1 ~$ n use 14.19.0 index.js Node version: v14.19.0 ~$ n use 12.4.0 index.js Error: '12.4.0' is not installed ~$ node -v v16.18.1
In the example above, the active version of Node is v16.18.1. When n use 14.19.0 index.js
is run, the output indicates that the version of Node used to execute the script was 14.19.0. After execution, the active version of Node is still v16.18.1. Note that the n use
command requires the requested version of Node to be installed by n
already.
This capability can be useful in certain situations. For example, think of a build server used to build different apps with their own required Node versions. Each build could be triggered with the n use
command, specifying the required Node version for that application.
Let’s face it: switching between Node.js versions when working on multiple projects can be a pain, and that’s not considering the amount of time you might waste searching for a non-existent bug caused by using the wrong Node.js version or having to remember multiple node versions.
Automating this process will not only save you time, but it’ll also help you get started with your projects with a clear mind.
Although it may seem counterintuitive to set up an automation that might take you double the time it would take to switch to the preferred version and get to work, the potential time savings, in the long run, will outweigh the initial setup time.
Automating version switching is a fairly straightforward process, all you have to do is create a .nvmrc
file in your project directory and specify the preferred Node version. For example, if your project requires Node.js version 18.14.0
, you’ll add the following:
18.14.0
Alternatively, if you want your project to always use the latest version of Node, you can specify the following instead:
node -v > .nvmrc
Then, add the following script at the end of your ~/.bashrc
file:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion nvm_auto_use() { 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 } export -f nvm_auto_use PROMPT_COMMAND="nvm_auto_use; $PROMPT_COMMAND"
Or you can add the following script to your ~/.zshrc
if you’re using the Zsh terminal:
load-nvmrc() { local nvmrc_path nvmrc_path="$(nvm_find_nvmrc)" if [ -n "$nvmrc_path" ]; then local nvmrc_node_version nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")") if [ "$nvmrc_node_version" = "N/A" ]; then nvm install elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then nvm use fi elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then echo "Reverting to nvm default version" nvm use default fi } add-zsh-hook chpwd load-nvmrc load-nvmrc
These scripts will automatically check your shell for a .nvmrc
file in every directory you change to (cd into)
and switch to the specified Node.js version.
Now, run source ~/.zshrc
or source ~/.bashrc
to complete the setup.
This automation procedure only applies to Unix shell environments, which means the method won’t work on Windows-based systems because the .zshrc
and .bashrc
files aren’t directly available on a native Windows system.
To automate version switching on Windows, you can use third-party tools like AVN, NVM Shim, or the VS Code NVM switcher extension. Note that these tools also require you to add an .nvmrc
file in the root folder of your project with the preferred version specified, as described earlier.
n
Node version managerThe NVM for Windows and N tools share many features, but they also have unique aspects that influence how and when you should use each tool. Here is a summary of some of the key differences:
Capability | NVM for Windows | n |
---|---|---|
Installation | Windows installer or standalone installation | Bash script or npm package |
Operating system support | Windows (different implementations for Linux/Mac are available) |
Linux/Mac only |
List available versions of Node to install? | Yes | No |
List installed versions of Node? | Yes | Yes |
Install and switch between different Node versions? | Yes | Yes |
Access Node binary directly? | No | Yes |
Choose which architecture (x86, x64) to install? | Yes | Yes |
You may use n
on your Linux box because of its simple API. Or maybe you’ll choose NVM for Windows on your Windows box and n
on your Linux build server to manage Node versions between different build jobs.
Whatever the situation, both tools do a fantastic job of fulfilling the need to switch Node versions on the fly. Happy Node version switching!
There are several other Node version managers besides n
and NVM that offer similar functionalities and sometimes additional features. Here are a few popular options:
In this article, we explored two popular Node version managers: NVM for Windows and n
for Linux/Mac. Both offer efficient ways to switch between Node.js versions for your development needs. The article also provided a brief introduction to other options like Volta, FNM, asdf, and Nodist, giving you a broader perspective on available tools.
With the knowledge gained from this article, you can confidently select the right tool and manage your Node.js versions like a pro!
Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third-party services are successful, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.
LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowLearn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.
Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.
Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.
Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.
4 Replies to "Switching between Node versions during development"
Excellent article, very well written and explained, surely helps with the headache of dealing with multiple n versions, specially for new developers that are just getting familiarized with it. Keep them coming !
the names applcation1 and application2 what are they? are they file names or folder names, i typed the folder names of node.js app but it says cant find the specified path
Application 1 and 2 are folder names. They represent theoretical applications that run on Node. This post refers to application 1 as an Angular 5 application and application 2 as an Angular 7 application as an example of two different applications with different Node version requirements however they can be any type of application as long as each folder has a package.json. Hope that helps!
exec: “elevate.cmd”: executable file not found in %PATH%:
Getting this error. Please help me.