DEV Community

Shimju David
Shimju David

Posted on

Storing config settings of a Node.js application in an elegant way.

This post explains how to store your config settings for your node.js application and overwriting each settings based on different environments. For example, in dev environment we will use different database and mail server than in production environment.

using a .env file

A popular solution to how you can organize and maintain your environment variables is to use a .env file. I really like this technique as it makes it super easy to have one place where I can quickly read and modify them.

Custom-Env package:

Custom env is a library built to make development more feasible by allowing multiple .env configurations for different environments. This is done by loading environment variables from a .env.env name file, into the node's process.env object.

npm i custom-env
Enter fullscreen mode Exit fullscreen mode

Place this at the top of your application

// this will load .env file which is default dev mode
require('custom-env').env() 

// OR  this will load .env.staging file 
require('custom-env').env('staging')

// OR this will load .env.production file
require('custom-env').env('production')   

//OR this will load the current environment dynamically, but default to dev mode.
require('custom-env').env(true) 
Enter fullscreen mode Exit fullscreen mode

This by default loads configuration from the .env file and assumes the app is in development environment mode (dev).

Create a .env file in your app's root directory and add the environment variables each on new line:

APP_ENV=dev
DB_HOST=localhost
DB_USER=root
DB_PASS=root
Enter fullscreen mode Exit fullscreen mode

Simple! The process.env is now loaded with the environment variables above.

//index.js
console.log(process.env.APP_ENV)
console.log(process.env.DB_HOST)
console.log(process.env.DB_USER)
console.log(process.env.DB_PASS)

/* output
dev
localhost
root
root
*/
Enter fullscreen mode Exit fullscreen mode

Now create another .env for staging environment

Create a .env.staging file in your app's root directory and add the environment variables each on new line:

.env.staging
APP_ENV=staging
DB_HOST=staging.emaxsoftware.com
DB_USER=stage_user
DB_PASS=state_pwd
Enter fullscreen mode Exit fullscreen mode

If you want to load from a particular environment, use:

//index.js
// This loads configuration from staging environment
require('custom-env').env('staging')
Enter fullscreen mode Exit fullscreen mode

we can set environment variable using terminal

cd to project folder and

export NODE_ENV=staging
Enter fullscreen mode Exit fullscreen mode

Remember your .gitignore File

.env file is a great way to see all of your environment variables in one place. Just be sure not to put them into source control. Otherwise, your history will contain references to your secrets!

Create a .gitignore file (or edit your existing one, if you have one already) and add .env to it, as shown in the following image. The .gitignore file tells source control to ignore the files (or file patterns) you list.

Alt Text

Be careful to add .env to your .gitignore file and commit that change before you add your .env

Config file

If you want a dedicated config file to find the .env files, parse it, and read them into your Node.js app in an elegant way, then you will need one more npm package called dotenv.

npm i dotenv
Enter fullscreen mode Exit fullscreen mode

Now let’s create a new module in a file named config.js under webroot or inside a config folder (your preference).

// config.js
const dotenv = require('dotenv');
dotenv.config();
module.exports = {
  mode: process.env.APP_ENV,
  db_host: process.env.DB_HOST,
  db_user: process.env.DB_USER,
  db_pass: process.env.DB_PASS
};
Enter fullscreen mode Exit fullscreen mode

Now on index.js page on top

require('custom-env').env(true);
const { mode, db_host, db_user, db_pass } = require('./config');

console.log(`Your mode is ${mode}, dbhost is ${db_host} , dbuser is ${db_user} , dbpwd is ${db_pass} `);
Enter fullscreen mode Exit fullscreen mode

Now based on environment changes, config file will also grab the respective configurations from the respective .env files, parse it and make it available in your Node.js app.

Top comments (1)

Collapse
 
mgrachev profile image
Grachev Mikhail

Another useful tool - github.com/dotenv-linter/dotenv-li....
It’s a lightning-fast linter for .env files. Written in Rust.