NodeJS — Run your app with multiple versions of Node

Vishal Raj
2 min readJun 9, 2021

--

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 usewe 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
multi-node-app/
├── app
│ ├── index.js
│ └── package.json
├── docker-compose.yml
├── node14.Dockerfile
├── node16.Dockerfile
└── proxy
└── nginx.conf

2 directories, 6 files
vishalr@ubuntu ~>

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.

127.0.0.1 node16.myapp.local node14.myapp.local

To start all the containers execute the following command -

vishalr@ubuntu ~> docker-compose up

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

Originally published at https://dev.to on June 9, 2021.

--

--