How do manage environment based configuration node js/typescript

Any application needs configuration settings, the programming language like ASP.NET we typically use Web.config for configuration, for different environment we have separate files like

Web.Dev.Config,
Web.QA.Config,
Web.Staging.Config,
Web.Production.Config, etc.

In nodejs app I could not find any specific standard for this but there is a multiple option to setup this configurations, below I used .json file for different environment.

Folder Structure.

default.json – this will hold all the base configuration and common to all the environment
Sample:

<environment>.json – for every environment there will be a separate  .json file, this contain configuration specific to that particular environment, this will override our base default values if same key exist.
Sample: development.json

How its works?
In the index.js we getting  configuration files using require and merging default and respecting environment file then doing export.

process.env.NODE_ENV is environment value, we can set on respective server, for testing I used npm script to set this value in my package.json

Note: I tried with mac system, so setting environment value using “export NODE_ENV=production”, If you trying with windows may be need to change the command slightly like  “SET NODE_ENV=production”.

I used Typescript on the server, so during build copied all my configuration to deployment folder.

Okay, how to access these configuration inside application?

import the config and use wherever needs across the app, below snap  getting mongo db connection value using key “config.db.connection”

 

sample available on following path: https://github.com/arulselvan/practice/tree/master/nodejs/environment-based-configuration