A Few Ways to Handle Environment Config at Express.js
Table of Contents
Introduction
Configuration for your application is very important because your testing, development, and production environments are going to have different environment variables. This post shows a few options for managing configuration in Node.js and Express.js.
nconf
nconf is a very powerful configuration module in Node.js. It is a key-value store, pluggable, and able to be used with Redis.
Usage
When you use it, you can create different kinds of environment variable files.

One of the config files, development.json, might look like the following.

Write Your Own Config
Instead of using nconf, you can use the following JavaScript to write your own configuration.

Inside the config.js, use Lodash to merge the default config and the environment-specific config values.

You can use set NODE_ENV=development to change the Node.js environment variable for your application.
Conclusion
Managing environment configuration is essential for any Node.js application. You can use a powerful module like nconf for a feature-rich key-value store, or write your own simple config module using Lodash to merge default and environment-specific settings. Choose the approach that best fits your project's complexity.