DEV Community

loading...

NodeJS - Run your app with multiple versions of Node

Vishal Raj
Experienced with web development, interested in scalability & security.
・2 min read

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

127.0.0.1  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

Discussion (2)

Collapse
vishalraj82 profile image
Vishal Raj Author

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