When you are a NodeJS developer and you develop/run multiple applications on the local machine, this is a problem that you might have definitely encountered. This problem has become worse since new features are pouring into NodeJS and you have to keep up with the flow. For example, say you have currently few applications running on NodeJS version 4.x.x and you need to go for ES6 features using NodeJS 6.x.x . At this moment, you need both of these applications running on top of different Node.JS versions without using containers such as Docker in your local machine.
Multiple Node JS Versions with n
‘n’ can be installed using npm simply by running the following command. Depending on your operating systems and permissions, you might require
sudo in front of.
npm install -g n
After installing n, you can switch between different versions of NodeJS installations as follows. If you don’t have the requested version installed on your machine, n will automatically download it, install it and then will switch the version.
n 4.4.1 # this will install v4.4.1 n 6.9.2 n 7.0.0 n stable # this will install the latest stable version of node n latest # this will install the latest version of node
You can also list the install NodeJS versions in your machine running just
n, and then you can graphically switch between the installed versions.
Based on my experience, at the time of this writing, this tool works best in OSX installations, it was a bit tedious for me to get this work in Linux (Ubuntu, for an example). For Linux, I suggest using NVM for switching between versions (discussed later in this post). Please place a comment if you have experienced otherwise.
You can also dynamically change the NodeJS version for a particular application using n. Consider the following script:
# test.js console.log("Running this script as : " + process.version);
I can run this script
test.js using any NodeJS version using n as follows:
n use 7.0.0 test.js
This will provide the output:
Running this script as v7.0.0
Multiple Node JS Versions with NVM
NVM (Node Version Manager) can also be used to manage multiple NodeJS versions at the same time. You can install setup NVM in your machine by running the following commands. Following steps were extracted from the original documentation at nvm’s GitHub repository.
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
Then run the following command to enable
nvm command, if you are trying to use nvm in Linux.
If you are using Mac OS and use bash, you need to run the following command. If
~/.bash_profile file is not there, you might need to create it first.
Note! If you are using a different shell such as zsh, you'll have to run the
sourcecommand accordingly. e.g, for zsh it should be
To verify whether it has been successfully installed, run the following command which should output
nvm if it has been correctly installed
command -v nvm # should output 'nvm'
Once this is set up, you can install any NodeJS version as follows:
nvm install v4.4.1 #this will installed 4.4.1 nvm install v6.9.1 #this will install 6.9.1
Once the version is installed, you can switch to it anytime by running the following command. The output is also displayed here:
$ nvm use 4.4.1
Now using node v4.4.1 (npm v4.0.3)
Please note that NVM is only installed for the current user. It’s not exactly an application installed, but a set of shell commands. Therefore, those commands won’t be available to other users. If other users need to maintain different node versions, you need to install
nvmfor each user. Due to the same reason, one user’s
nvm versioncan be different from the other.
n you can use nvm to dynamically select a NodeJS version for a particular application. To run a script
test.js on any installed NodeJS version, you can run the command:
nvm run 4.4.1 test.js
You can also run it as:
nvm exec 4.4.1 node test.js
It would be tedious to always type the NodeJS version as above to run your application if you are simultaneously working on multiple projects which require different NodeJS versions. As a solution, you can leverage a
.nvmrcfile in your project.
Using a .nvmrc file in your project
Go to your project root and create a file with name
.nvmrc. Then specify your project’s NodeJS version in the
After setting up
.nvmrc file, all nvm command such as
nvm exec will obey to this configuration. Then you can simply run the commands as follows to start your app.
$ nvm run app.js Found '/Users/thejsblog/mynodeapp/.nvmrc' with version <v6.9.1> Running node v6.9.1 (npm v3.10.8)
nvm will detect the
.nvmrc file and run the script with the specified version. If you want to run
npm install for your required NodeJS version, you can run it as follows:
$ nvm exec npm install
Found '/Users/thejsblog/mynodeapp/.nvmrc' with version <v6.9.1> Running node v6.9.1 (npm v3.10.8)
You can visit NVM documentation to discover more on this tool. I prefer using
nvm in both MacOS and Linux. In MacOS, I suggest using
nvm because, at the time of this writing, it is a bit hard to get
n working on Linux.