Config module for node


Config module for node

Via npm:

$ npm install nconfig
var path = require('path');
var options = {
    "path": path.join(__dirname, 'config');
var nconfig = require('nconfig')(options);
var config = nconfig.loadSync('development');

path (string) — Sets the directory path of the configuration files.

Loads the configuration file synchronously.


  • env (string) — Environment name. Synonymous to the configuration file's base name.
  • mergeWithEnv (string or array) — If specified, nconfig will load and merge the listed configurations (read below).

Config files should be in JSON format with a .json extension.


// development.json 
    "name": "My App",
    "db": {
        "host": "localhost",
        "user": "root",
        "pass": "abc123"

If the mergeWithEnv parameter was specified, nconfig will load and merge config files based on the following rules:

  • The left-most environment's config will have the highest priority while the right-most will have the lowest. Lower priority config properties will be overwritten by config properties of higher priority.
  • Any environment listed to the left of the target environment will be ignored (e.g. ["production", "beta", "testing", "development"] with "beta" as target will not load "production"; "beta" will have the highest priority; "development" will have the lowest priority)
  • Same named properites of type object will have their sub-properties merged.
  • If a string was used in mergeWithEnv, that config will be loaded with lowest priority and will be overwritten by the target environment's config.
  • If the target environment is not an element of the mergeWithEnv array, it will follow the same logic as above with the target environment's config having the highest priority. All of the configs listed in the array will be loaded.
  • Environments with no config files will return { }.

MIT License. Read the LICENSE file.

In summary, you can do whatever you want with the code but if it breaks, sorry :(

If you find bugs or think some parts can be improved, please post the request on GitHub.

If you want to fix it yourself, kudos! Fork the repo, do your magic, and invoke a pull request. I'll be sure to thank you and add you here as a contributor.