• Gaurav Umrani

Config Module- Cleaner way to write NodeJS configuration files

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

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

For example: 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 test.paypal.com but if you are on the production, it will be secure.paypal.com.

Generally developers configure these setting like this:

config.url = ‘test.paypal.com’;
if (process.env.NODE_ENV === ‘production’) {
config.url = ‘secure.paypal.com’;

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.

So how to overcome this problem?

To avoid this mess, we can use config module. This config module organises 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 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.

If you find this article useful then please leave a like and share within your network and if you have suggestions or questions, comment section is waiting for you 🙂 🙂

  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest