Editor’s note: This article was last reviewed and updated on 31 January 2023. For more information on switching Node versions using nvm, check out this article.
Sometimes it seems like there are new versions of Node.js released almost weekly — minor versions every few weeks, and major versions every few months. If you are a developer that needs to switch between different applications and projects on a regular basis, 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. Application 1 is an Angular 12 app running on Node 14.19.0. Application 2 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.
Jump ahead:
n
Node version managerTechnically, there are two completely separate nvm projects that offer similar capabilities on different operating systems but are maintained independent 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. At the time of writing, 1.1.10 is the latest release. 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.
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.Tip: Detailed installation instructions are available on GitHub.
Once installation is complete, open a command window and confirm 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 be usednvm 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 provides a simpler CLI for installing and switching between Node versions. It is only supported on Linux or Mac operating systems.
Tip: Detailed installation and usage instructions are available in the tj/n repository on GitHub.
If you have a version of Node and npm installed already, you can install n
just like any other npm package using 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 is 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.
n
Node version managernvm for Windows and n
have many common features and some unique features that affect how and when you 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 implementation for Linux/Mac is 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 choose to 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 may be, both of these tools do a fantastic job of fulfilling the need to be able to switch Node versions on the fly. Happy Node version switching!
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.
Vite is a versatile, fast, lightweight build tool with an exceptional DX. Let’s explore when and why you should adopt Vite in your projects.
Explore advanced capabilities for content sharing with the navigator.share
API, including dynamic content sharing, custom share targets, and batch sharing.
We spoke with Chas to get his insights on building technology and internal processes for companies that are scaling quickly.
Cypress is one of today’s foremost tools for testing web applications. Let’s explore when and why you should adopt Cypress in your projects.
3 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!