config.json

nconf wrapper that simplifies work with environment specific configuration files.

npm install config.json
3 downloads in the last day
13 downloads in the last week
74 downloads in the last month

config.json

nconf wrapper that simplifies work with environment specific configuration files.

Build Status Dependency Status

config.json is easy to use; it:

  • loads the default configuration file;
  • loads environment specific configuration file and overrides defaults;

and then:

  • uses environment variables;
  • and command-line arguments to override data from configuration files.

Installation

$ npm install config.json

Usage

The top-level of config.json is a function that loads configuration file with the given filepath.

Create default configuration file

$ vi sample.json
{
  "domain": "www.example.com",
  "mongodb": {
    "host": "localhost",
    "port": 27017
  }
}

Create environment specific configuration file

$ vi sample.development.json
{
  "domain": "dev.example.com"
}

Note: Environment specific configuration files should be in the same directory as the default one.

Test config.json in action

$ vi sample.js
var config = require('config.json')('./sample.json');

console.log("domain:", config.domain);
console.log("mongodb:\n",
  "host:", config.mongodb.host, "\n",
  "port:", config.mongodb.port);

Run the above script:

$ NODE_ENV=development node ./sample.js --mongodb:host "dharma.mongohq.com" --mongodb:port 10065

The output will be:

domain: dev.example.com
mongodb:
 host: dharma.mongohq.com
 port: 10065

Load configuration for the specific environment

Environment can be set by passing env argument:

var developmentConfig = require('config.json')('./sample.json', 'development');
var productionConfig = require('config.json')('./sample.json', 'production');

One more thing...

filepath can be empty if your configuration file is in the current working directory of the process and is called config.json.

License

(The MIT License)

Copyright (c) 2014 Romuald Bulyshko <romuald@bulyshko.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you