Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including no-config with all npm packages installed. Try it out:

var noConfig = require("no-config")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

no-config v1.1.2

config and resource loader

NPM version Build Status Test Coverage Dependency Status Dependency Status
Intro | Quick start | Quick start (ES6) | API | Contributors | TODO

Intro

Why not config?

Answer. TL;DR: config separates data to different files based on NODE_ENV, not resources.

$ npm install no-config

Quick start

// config.js
module.exports = {
    redis: {
        init: function (params) {
            return require('redis').createClient(params)
        },
        default: {
            db: 0,
            port: 6379
        },
        development: {
            host: '127.0.0.1'
        },
        production: {
            db: 1,
            host: '192.168.0.10'
        }
    }
}
// index.js
require('no-config')({
    config: require('./config')
}).then(
    function(conf) {
        console.log('ENV', conf.env)
        console.log('Redis:', conf.redis.host+':'+conf.redis.port)
        conf.redis.instance.set('hello', 'world')
    }
)
$ NODE_ENV=development node index.js
ENV development
Redis: 127.0.0.1:6379

Quick start (ES6)

Since no-config returns a promise it is much better to use ES6 generators, arrow functions and co.
If you are not familiar with co, check this step-by-step tutorial

// config.js
module.exports = {
    redis: {
        init: params => require('redis').createClient(params),
        default: {
            db: 0,
            port: 6379
        },
        development: {
            host: '127.0.0.1'
        },
        production: {
            db: 1,
            host: '192.168.0.10'
        }
    }
}
// index.js
'use strict'
const co = require('co')
co(function* () {
    let config = require('./config')
    let conf = yield require('no-config')({config})

    console.log('ENV', conf.env)
    console.log('Redis:', conf.redis.host+':'+conf.redis.port)
    conf.redis.instance.set('hello', 'world')
})

API

Loader

require('no-config')(parameters)

Loads resources from parameters.config based on NODE_ENV environment variable. Returns a Promise which resolves ones all resources are initialized.

Parameters

NameRequired?TypeDefaultDescription
configRequiredObjectConfiguration object
initOptionalList of stringsAll ResourcesResources to initialize
verboseOptionalBooleanfalsePrint resource input prior to call its init() function
mask_secretsOptionalBooleantrueif verbose === true will hide input value if its key contains substrings: secret, token, key, pass or pwd

Configuration object

Every high-level key in configuration object is a resource name.

NameRequired?TypeDefaultDescription. Handling
<RESOURCE>OptionalObjectResource configuration
<RESOURCE>.defautOptionalObject{}Default values
<RESOURCE>.<ENV>OptionalObject{}ENV specific values. If a key duplicates default key, env-specific value is used
<RESOURCE>.initOptionalFunction, Generator functionCalled to initalize resource, <RESOURCE>.init(result). If returns Promise or Generator, it got resolved with co. Result is saved to result.instance.

Contributors

Fedor Korshunov - view contributions
Anurag Sharma - view contributions

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free