Config Module - Cleaner Way to Write NodeJS Configuration Files

Engineering Feb 16, 2019

What Is Configuration Files?

configuration files (or config files) are files used to configure the parameters and initial settings for some computer programs:- Wikipedia

Before starting to write software, we require few initial settings or files such as config files. These config entries are different every time according to deployments.

What Is Deployment?

Software deployment is all of the activities that make a software system available for use. The general deployment process consists of several interrelated activities with possible transitions between them. These activities can occur at the producer side or at the consumer side or both- Wikipedia

Configuration and Deployment

If you have a payment gateway in your application then URL for the payment gateway will be stored in the config file, but the given URL will vary according to deployment. If you are on the development server then URL will be but if you are on the production, it will be

Generally developers configure these setting like this:

config.url = ‘’;
if (process.env.NODE_ENV === ‘production’) {
config.url = ‘’;

This is fine until you are dealing with less deployments but when your application starts introducing more deployments such as development, staging, production, testing etc., then the code structure become messy.

How to Overcome This Problem?

To avoid this mess, we can use config module. This config module organizes hierarchical configurations for your app deployments, it means that it allows you to create configuration files for each deployment and it will automatically pick the configuration file according to the deployment with a certain order.

Let’s see how can we use this module in our application:

1. Install the config module first

npm i config — save

2. This module looks for config files in config directory at root level. You can also change it to another directory by adding one line code at top of your app.js

process.env['NODE_CONFIG_DIR'] = __dirname + '/configDir/';

3. Config files are loaded in following order:

(Finally, custom environment variables can override all files)

Here I am showing .json format extension but this module also supports other extensions such as .yml, .yaml, .xml, .coffee, .cson, .properties, .json, .json5, .hjson, .ts, .js

Note: Other than above files the module searches for more deployments and you can see the complete list here

The module chooses deployment from NODE_ENV environment variable if the value of NODE_ENV is development then development.json will be used, similarly if NODE_ENV is production then production.json will be used and if your config folder has the local.json file then the local file will be used.

Important: Please note that local files should not be pushed to your repository as local.json files are recommended to be used only in your local development.

4. Now assume we have 2 configuration files, one for local development and one for production

 'dbUrl': 'mongodb://localhost:27017/mydb'
 'dbUrl': 'mongodb://some-db-location/dbname'

then write this piece of code in your project

var config = require(‘config’);

So, If you are developing locally, then the value for dbUrl will be taken from local.json and when you are in the production environment (NODE_ENV = production), then the value will be automatically taken from production.json

This approach solves your problem to configure multiple files according to deployment needs.

What’s the biggest thing you’re struggling with right now that we as a technology consulting company can help you with? Feel free to reach out to us at We hope our assistance will help!


Gaurav Umrani

Product Engineer | JavaScript | Angular | Node | React | Go

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.