DEV Community

Vishal Raj
Vishal Raj

Posted on


NodeJS - Run your app with multiple versions of Node

As newer version of NodeJS is released, it brings with it - performance improvements, speed, security, new features and more. If you have a nodejs based web application and plan to upgrade the version of nodejs, then of course it becomes important to test the application on the new version to ensure its sanity.

In this post we will explore how can we use Docker to run our nodejs based application with two (or more) versions of nodejs.

Lets explore the directory structure to understand how the files have been organized.

vishalr@ubuntu ~> tree multi-node-app
├── app
│   ├── index.js
│   └── package.json
├── docker-compose.yml
├── node14.Dockerfile
├── node16.Dockerfile
└── proxy
    └── nginx.conf

2 directories, 6 files
vishalr@ubuntu ~> 
Enter fullscreen mode Exit fullscreen mode

The main app resides inside the app folder. The files node14.Dockerfile and node16.Dockerfile contain instructions to build docker image, to run app with Node v14.x and v16.x. The file docker-compose.yml is a wrapper over the two docker files and adds Nginx as proxy over two docker containers. The file proxy/nginx.conf contains the barebones configuration to use nginx as proxy for our application.

Additionally we also need to make the following entry in the file /etc/hosts.  node16.myapp.local  node14.myapp.local
Enter fullscreen mode Exit fullscreen mode

To start all the containers execute the following command -

vishalr@ubuntu ~> docker-compose up
Enter fullscreen mode Exit fullscreen mode

Once all the containers (nginx, node16 and node14) are up, you can use the urls http://node16.myapp.local and http://node14.myapp.local in your local browser to test your application running with Node v16.x and Node v14.x respectively.

You can find this project at my Github repository

Top comments (2)

vishalraj82 profile image
Vishal Raj

Thanks. Well, I wrote the article because I was myself in the situation to port my app between different node versions.

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.