Skip to main content

Switching between Node versions during development

Switching Between Node Versions During Development
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

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.zip and extract it wherever you would like. Run the included install.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.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
> application1@0.0.0 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 available provided a convenient partial list of Node versions available to be installed
  • nvm install installed the required Node version (a 64-bit version by default since the current system’s architecture is 64-bit)
  • nvm use activated the version that was just installed
  • nvm list confirmed 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/cli@8
...
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 v8.16.0 so that you could verify that the application worked as expected before the upgrade
  • nvm install and nvm use installed and activated v10.16.0 in preparation for the upgrade
  • Globally installed the @angular/cli package to get access to the ng update command that automatically upgrades Angular applications
  • npm install and npm start to 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.

Installation

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
> application1@0.0.0 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: 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/cli@8
...
~/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 <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 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 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 nuse command, specifying the required Node version for that application.

Summary comparison

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:
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 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.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...