DEV Community


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

shimjudavid profile image Shimju David ・3 min read

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

Place this at the top of your application

// this will load .env file which is default dev mode

// OR  this will load .env.staging file 

// OR this will load .env.production file

//OR this will load the current environment dynamically, but default to dev 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:


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


/* output

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:


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

// This loads configuration from staging environment

we can set environment variable using terminal

cd to project folder and

export NODE_ENV=staging

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

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');
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

Now on index.js page on top

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} `);

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.


Editor guide
mgrachev profile image
Grachev Mikhail

Another useful tool -
It’s a lightning-fast linter for .env files. Written in Rust.