Node.js: Setup Different Configuration Settings for Development & Production Server

This article shows how you can set up different configuration files for different servers like a production server or development server.

For example, we can have different database configuration settings for development server and production server. If we write the database config settings (hostname, username, password) in the single file then we have to manually change them in development and production server separately.

To make this more systematic, there can be different solutions. Here, I will discuss two Node.js modules that can be used to store the configuration settings. Hence, easing up the app deployment process.

I have created a folder named “nodejs” inside my Documents folder. And, then created a simple Node.js app over there.


mukesh:nodejs chapagain$ pwd
/Users/mukeshchapagain/Documents/nodejs

Using dotenv module

Dotenv is a Node.js module that loads environment variables from a .env file into process.env.

Using this module, you can have different content in the .env file present in your development and production server.

We will install the dotenv module to access the .env file contents.


npm install dotenv --save

My application files,

– the main application file is app.js
– the environment variables are stored in .env file

Note:

Remember to ignore the .env file in your git repository by creating a .gitignore file and adding the .env line over there. Otherwise, your private information stored in the .env file might get exposed.

dotenv module is used to include the .env file in our main app js file.

.env


DB_HOST=localhost
DB_USER=yourDbUsername
DB_PASS=yourDbPassword

app.js


require('dotenv').config(); // include .env file

let db = {
    host: process.env.DB_HOST,
    username: process.env.DB_USER,
    password: process.env.DB_PASS
};

console.log(db);

Run the application:


node app.js

Output


mukesh:nodejs chapagain$ node app.js
{ host: 'localhost',
  username: 'yourDbUsername',
  password: 'yourDbPassword' }

Using config module

Config is a Node.js module that lets you define a set of default parameters in a configuration file. That configuration file can then be extended for different deployment environments (development, qa, staging, production, etc.).

Install the config module


npm install config --save

Create a config folder


mkdir config

Create default.json file inside the config folder


touch config/default.json

Add the following code to config/default.json file

config/default.json


{
  "Settings": {
    "dbConfig": {
      "host": "localhost",
      "port": 5984,
      "dbName": "yourDatabaseName"
    },
    "site": {
      "url": "http://localhost.com",
      "myVar": 10
    }
  }
}

In the above code, we can also set the values in environment variables and use them in the above JSON file.

config/default.json


{
  "Settings": {
    "dbConfig": {
      "host": process.env.DB_HOST || "localhost",
      "port": parseInt(process.env.APP_PORT) || 5984,
      "dbName": process.env.DB_NAME || "yourDatabaseName"
    },
    "site": {
      "url": process.env.SITE_URL || "http://localhost.com",
      "myVar": 10
    }
  }
}

app.js


var config = require('config');

var settings = config.get('Settings');
var dbConfig = config.get('Settings.dbConfig');
var site = config.get('Settings.site');
var siteUrl = config.get('Settings.site.url');

console.log('\n SETTINGS \n');
console.log(settings);

console.log('\n DBCONFIG \n');
console.log(dbConfig);

console.log('\n SITE \n');
console.log(site);

console.log('\n SITEURL \n');
console.log(siteUrl);

Run the application:


node app.js

Output


mukesh:nodejs chapagain$ node app.js
WARNING: NODE_ENV value of 'default' is ambiguous.
WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode

 SETTINGS

{ dbConfig: { host: 'localhost', port: 5984, dbName: 'yourDatabaseName' },
  site: { url: 'http://localhost.com', myVar: 10 } }

 DBCONFIG

{ host: 'localhost', port: 5984, dbName: 'yourDatabaseName' }

 SITE

{ url: 'http://localhost.com', myVar: 10 }

 SITEURL

http://localhost.com

DIFFERENT CONFIGURATION FOR PRODUCTION SERVER

The above code fetches the configuration settings from the config/default.json file. You can have different configuration settings for the production server.

In that case, you need to create a new file named “production.json” inside the “config” folder.

  • The production.json file can override the default.json.
  • So, you need not have to write everything there.
  • You can just write the things that are different in production than the default one.

Create production.json file inside the config folder


touch config/production.json

Add the following code to config/production.json file

Here, we are only overriding the dbName and site URL of the default.json file.

config/production.json


{
  "Settings": {
    "dbConfig": {
      "dbName": "productionDb"
    },
    "site": {
      "url": "http://example.com"
    }
  }
}

JSON file with the environment variables.

config/production.json


{
  "Settings": {
    "dbConfig": {
      "dbName": process.env.DB_NAME || "productionDb"
    },
    "site": {
      "url": process.env.SITE_URL || "http://example.com"
    }
  }
}

app.js

app.js file will be the same.


var config = require('config');

var settings = config.get('Settings');
var dbConfig = config.get('Settings.dbConfig');
var site = config.get('Settings.site');
var siteUrl = config.get('Settings.site.url');

console.log('\n SETTINGS \n');
console.log(settings);

console.log('\n DBCONFIG \n');
console.log(dbConfig);

console.log('\n SITE \n');
console.log(site);

console.log('\n SITEURL \n');
console.log(siteUrl);

Set environment variable NODE_ENV as production.


export NODE_ENV=production

Run the application


node app.js

Output


mukesh:nodejs chapagain$ node app.js

 SETTINGS

{ dbConfig: { host: 'localhost', port: 5984, dbName: 'productionDb' },
  site: { url: 'http://example.com', myVar: 10 } }

 DBCONFIG

{ host: 'localhost', port: 5984, dbName: 'productionDb' }

 SITE

{ url: 'http://example.com', myVar: 10 }

 SITEURL

http://example.com

In the output, you can see that the dbName has been changed in the dbConfig. Also, the Site url has changed. Those values are taken from config/production.json file. Rest of all the values are taken from config/default.json file.

You can also specify the environment variable NODE_ENV in your app.js file. If you define it in the app.js file then it will override the value you have set while exporting the environment variable.

process.env.NODE_ENV = “production”;

app.js


process.env.NODE_ENV = "production";

var config = require('config');

var settings = config.get('Settings');
var dbConfig = config.get('Settings.dbConfig');
var site = config.get('Settings.site');
var siteUrl = config.get('Settings.site.url');

console.log('\n SETTINGS \n');
console.log(settings);

console.log('\n DBCONFIG \n');
console.log(dbConfig);

console.log('\n SITE \n');
console.log(site);

console.log('\n SITEURL \n');
console.log(siteUrl);

Hope this helps. Thanks.