Sometimes it seems like there are new versions of Node.js released almost weekly — minor versions every few weeks, 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 decent 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.
Tip: 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 5 app running on Node 6.17.1. Application 2 is an Angular 7 app running on Node 8.16.0. Here is what you need to accomplish:
- Fix bug x on Application 1
- Upgrade Application 2 to Angular 8
You are actually going to need three versions of Node to complete your tasks since the Angular 8 upgrade will require you upgrade Application 2 to Node 10.9 or greater.
NVM for Windows
Technically, there are two completely separate NVM projects that offer similar capabilities on different operating systems but are maintained independent of each other:
- nvm-sh/nvm is a bash script that can be used to manage Node versions on Linux and Mac
- coreybutler/nvm-windows is a Windows application (with or without an installer) that can be used to managed Node versions on Windows
This article focuses on NVM for 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.7 (Aug. 2018) is the latest release. Download and extract
nvm-setup.zip and double-click to 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.zipand extract it wherever you would like. Run the included
install.cmdto 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.7
Getting Application 1 running
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 getting Application 1 running first. Some command output has been truncated (
...) to save space.
D:\>nvm list available | CURRENT | LTS | OLD STABLE | OLD UNSTABLE | |--------------|--------------|--------------|--------------| | 12.4.0 | 10.16.0 | 0.12.18 | 0.11.16 | ... D:\>nvm install 6.17.1 Downloading node.js version 6.17.1 (64-bit)... Complete Creating C:\Users\Brian\Downloads\nvm-noinstall\temp Downloading npm version 3.10.10... Complete Installing npm v3.10.10... Installation complete. If you want to use this version, type nvm use 6.17.1 D:\>nvm use 6.17.1 D:\>nvm list * 6.17.1 (Currently using 64-bit executable) D:\>node -v v6.17.1 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 get the application running:
nvm list availableprovided a convenient partial list of Node versions available to be installed
nvm installinstalled the required Node version (a 64-bit version by default since the current system’s architecture is 64-bit)
nvm useactivated the version that was just installed
nvm listconfirmed that the correct version of Node was installed and activated (other installed versions would be listed if they existed)
Once 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.
Getting Application 2 running
So you have fixed bug x in Application 1, and now you are ready to tackle upgrading Application 2 to Angular 8:
D:\nvm install 8.16.0 ... D:>nvm use 8.16.0 Now using node v8.16.0 (64-bit) D:>cd application2 D:\application2>npm install ... D:\application2>npm start ... D:\application2>nvm install 10.16.0 ... D:\application2>nvm use 10.16.0 Now using node v10.16.0 (64-bit) D:\application2>npm i -g @angular/[email protected] ... 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 useinstalled and activated v8.16.0 so that you could verify that the application worked as expected before the upgrade
nvm useinstalled and activated v10.16.0 in preparation for the upgrade
- Globally installed the
@angular/clipackage to get access to the
ng updatecommand that automatically upgrades Angular applications
npm startto test the newly upgraded application
n Node version manager
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://git.io/n-install | bash ... === n successfully installed. The active Node.js version is: v10.16.0 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/10.16.0
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.
Getting Application 1 running
Application 1 requires Node v6.17.1, so you need to install that first, then run the app.
~$ n 6.17.1 install : node-v6.17.1 mkdir : /home/brian/n/n/versions/node/6.17.1 fetch : https://nodejs.org/dist/v6.17.1/node-v6.17.1-linux-x64.tar.gz ####################################################################################################################################### 100.0% installed : v6.17.1 ~$ node -v v6.17.1 ~$ 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 6.17.1. 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 be run as usual.
Tip: Similar to NVM, Node versions are completely isolated from each other. For example, globally installed packages are not shared between Node versions.
Getting Application 2 running
Next up, you need to get Application 2 running and proceed with the Angular 8 upgrade:
$ n 8.16.0 ... $ cd application2 ~/application2$ npm install ... ~/application2$ npm start ... ~/application2$ n 10.16.0 ... ~/application2$ npm i -g @angular/[email protected] ... ~/application2$ ng update @angular/cli @angular/core ... ~/application2$ npm install ... ~/application2$ npm start ...
Node v8.16.0 was installed to make sure Application 2 is working prior to the upgrade. Then, Node v10.16.0 is installed as required by Angular 8. 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.
Using a Node binary directly
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 v8.16.0 ~$ n use 10.16.0 index.js Node version: v10.16.0 ~$ n use 12.4.0 index.js Error: '12.4.0' is not installed ~$ node -v v8.16.0
In the example above, the active version of Node is v8.16.0. When
n use 10.16.0 index.js is run, the output indicates that the version of Node used to execute the script was 10.16.0. After execution, the active version of Node is still v8.16.0. 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.
NVM for Windows and n have many common features and some unique features that affect how and where you use each tool. Here is a summary of some of the key differences:
You may choose to use n on your Linux box because of its simple API. Or maybe you choose NVM for Windows on your Windows box and n on your Linux build server, and you use 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!
200’s only Monitor failed and slow network requests in productionDeploying 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. https://logrocket.com/signup/
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.