Configuring multiple environments with Nodejs

Configuring multiple environments with Nodejs

In this post we will look into the steps to configure multiple environments with nodejs. This is important because you would
want to test you application as per the configuration of corresponding environments such as dev, test or prod.

Introduction

The configuration of multiple environments with nodejs is pretty straightforward. We are going to use a library called
dotenv. For our demo we are going to use npm to install packages and run our application.

You can find the details of the dotenv package from the link below.

https://www.npmjs.com/package/dotenv

Understanding how the environment variables are stored

The environment variables are stored in a file named .env. Since this file would contain the URLS and other sensitive data for your environment it is not committed into the repo. Typically you would find that this file is added in the .gitignore file to avoid it to be committed.

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

You can see the naming convention of the environment files. It starts with .env followed by your environment name.

Loading the configuration with dotenv

A typical nodejs application has a server.js file that spins up your server listening to the incoming requests.

const http = require('http')
const app = require('./app')
const port = process.env.PORT || 3000
const server = http.createServer(app)

require('dotenv').config()

server.listen(port,() => console.log(`Server listening on ${process.env.BASE_URL}`))

The line require('dotenv').config() makes aware of all the existing .env files in the application and loads it into process.env.

Running the application specific to an environment

As seen above we have specified all the different environment files and also our server is aware of the different configurations present in the application.

It is now time to run the application with a specific environment such that the settings pertaining to that environment is loaded into the process.env which will be used to provide the settings to the application.

In order to run the application we need to add different scripts in our package.json file so that the configuration
is provided to the process.env when the application is running.

The below example shows the way to specify scripts specific to environment.

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon -r dotenv/config src/server.js",
    "dev": "nodemon -r dotenv/config src/server.js dotenv_config_path=env/.env.dev",
    "prod": "nodemon -r dotenv/config src/server.js dotenv_config_path=env/.env.prod",
    "local": "nodemon -r dotenv/config src/server.js dotenv_config_path=env/.env.local"
  },

When we run the application with the specific script in the command the environment specific settings is applied to the application.

Run the app in dev mode - npm run dev Run the app in prod mode - npm run prod Run the app in local mode - npm run local

This is all about configuring your nodejs application for multiple environments.

Thank you for reading and see you in the next post.

Buy a coffee for sudshekhar