Open In App

Provide some example of config file separation for dev and prod Environments

Improve
Improve
Like Article
Like
Save
Share
Report

Config files are used to store the URLs, environment-related credentials, etc for our application. In order to access the config files it is better to install the https://www.npmjs.com/package/dotenv npm package and place it in the root file where your application starts. This is used to detect if any .env values are passed or not.

Usually, all config files are stored in .env variables where setting up and handling those variables individually for the environment becomes a bit tricky

Instead, we can have a config.json file alone as per the folder structure below

 

config.json: We can use the config.json file as follows.

Javascript




const _ = require('lodash');
const config = require('./key/config.json');
  
let finalConfig = {};
  
const environment = {
  config_id: process.env.NODE_ENV || 'local',
};
  
const defaultConfig = config.default;
const environmentConfig = config[environment.config_id];
finalConfig = _.merge(defaultConfig, environmentConfig);
  
global.gConfig = finalConfig;


In the above example, an npm utility package called dash is used in order to merge the configs. As per the above example, we need to set only the NODE_ENV in the .env file 

config.json: And our config.json file will be as follows.

XML




{
  "local": {
    "config_id": "local",
    "node_port": 3000,
    "database_connection_string": "mongodb://localhost:27017",
    "redis_port": 6379,
    "redis_connection_host": "localhost",
    "JWT_PUBLIC_KEY": "./auth/key/access_key/jwt_rs256.key.pem",
    "cross_service_url": "http://localhost:8001/cross_service"
  },
  "dev": {
    "config_id": "dev",
    "node_port": 3000,
    "database_connection_string": "mongodb://22.34.54.21:27017",
    "redis_port": 6379,
    "redis_connection_host": "22.34.54.11",
    "JWT_PUBLIC_KEY": "./auth/key/access_key/jwt_rs256.key.pem",
    "cross_service_url": "http://1.108.151.123:8001/cross_service"
  },
  "prod": {
    "config_id": "prod",
    "node_port": 3000,
    "database_connection_string": "mongodb://3.44.56.67:27017",
    "redis_port": 6379,
    "redis_connection_host": "13.34.36.67",
    "JWT_PUBLIC_KEY": "./auth/key/access_key/jwt_rs256.key.pem",
    "cross_service_url": "http://6.238.341.125:8001/cross_service"
  }
}


Just by passing the NODE_ENV such as local, dev or prod we would be able to access the respective environment variables via global configs as per the below example which is used to access the redis host, by setting the NODE_ENV as local in the .env file

 


Javascript




const REDIS_HOST = global.gConfig.redis_connection_host;
  
console.log(REDIS_HOST)
  
// localhost


Make sure in the .gitignore file the config folder and .env are placed and based on the environment we can specify the values in .env and the config.json value will be mapped as per the value in .env.



Last Updated : 02 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads