Home

Awesome

confit

Build Status

Simple, environment-based configuration. confit loads a default JSON configuration file, additionally loading environment-specific files, if applicable. It will also process the loaded files using any configured shortstop protocol handlers—see Options below.

confit adds support for adding JavaScript-style comments in your json files as each file is processed by shush before being merged into your config.

Usage

const confit = require('confit');

confit([options])

'use strict';

const path = require('path');
const confit = require('confit');

const basedir = path.join(__dirname, 'config');
confit(basedir).create(function (err, config) {
    config.get; // Function
    config.set; // Function
    config.use; // Function

    config.get('env:env'); // 'development'
});

config factory

// All methods besides `create` are chainable
confit(options)
    .addDefault('./mydefaults.json')  //or .addDefault({foo: 'bar'})
    .addOverride('./mysettings.json') //or .addOverride({foo: 'baz'})
    .create(function (err, config) {
        // ...
    });

// - or -
//
// const factory = confit(options);
// factory.addOverride('./mysettings.json');
// factory.create(function (err, config) {
//     // ...
// });

Options

'use strict';

const path = require('path');
const confit = require('confit');
const handlers = require('shortstop-handlers');


const options = {
    basedir: path.join(__dirname, 'config'),
    protocols: {
        file: handlers.file(__dirname),
        glob: handlers.glob(__dirname)
    }
};

confit(options).create(function (err, config) {
    // ...
});

Config API

config.set('foo', 'bar');
config.get('foo'); // 'bar'

config.use({ foo: 'baz' });
config.get('foo'); // 'baz'

config.use({ a: { b: { c: 'd' } } } );
config.get('a:b:c'); // 'd'

Default Behavior

By default, confit loads process.env and argv values upon initialization. Additionally, it creates convenience environment properties prefixed with env: based on the current NODE_ENV setting, defaulting to development. It also normalizes NODE_ENV settings so values starting with prod become production, starting with stag become staging, starting with test become test and starting with dev become development.

// NODE_ENV='dev'
config.get('NODE_ENV');        // 'dev'
config.get('env:env');         // 'development'
config.get('env:development'); // true
config.get('env:test');        // false
config.get('env:staging');     // false
config.get('env:production');  // false
// NODE_ENV='custom'
config.get('NODE_ENV');        // 'custom'
config.get('env:env');         // 'custom'
config.get('env:development'); // false
config.get('env:test');        // false
config.get('env:staging');     // false
config.get('env:production');  // false
config.get('env:custom');      // true

Precedence

Precedence takes the following form (lower numbers overwrite higher numbers):

  1. command line arguments
  2. env variables
  3. environment-specific config (e.g., development.json)
  4. main config (config.json)
  5. env normalization (env, env:development, etc)

Shortstop Handlers

Confit by default comes with 2 shortstop handlers enabled.

{
    "foo": "import:./myjsonfile"
}
{
    "foo": {
        "bar": true
    },
    "foobar": "config:foo.bar"
}