lean-config v2.0.0
Leaner Config
Compact, flexible and least opinionated config loader.
- config parameters are stored in the files (
.js
and/or.json
), not in one - they are scattered across many; - so you can isolate task specific parameters in different files: i.e.
app.js
,mongodb.json
,stripe.js
and so forth; - the files lying in the root of the
config/
folder contains "general" parameters; - the files placed under sub-folders contains some redefined parameters specific for distinct activity;
- specific sub-folder to load can be set via NODE_ENV variable, by the config load function parameter or it can be explicitly disabled;
- first, the general configs are being loaded, second the specific configs loaded and overrides the general;
- multiple environments like "production,new-york-servers" can be set;
- finally everything is being combined into one pure javascript object;
- 70 LoC, no dependencies.
Quick start
Do NOT use npm - otherwise you have to configurate configuration rig, that's awkward.
Instead just download config/index.js
into your config folder.
You can ever include something like this:
"scripts": {
"postinstall": "wget -P config/ https://raw.github.com/googgah/lean-config/master/config/index.js"
}, ...
into your package.json
to automatically download it after npm install ...
Usage
Load only general configs (assuming NODE_ENV is not set):
const config = require( './config' );
// ...
app.listen( config.port, '0.0.0.0', () => {
console.log( ` • API: ${config.name} (v${config.version})` );
// ...
} );
Explicitly load general config (ignoring NODE_ENV):
const config = require( './config' ).load( false );
Explicitly load specific config (ignoring NODE_ENV):
const config = require( './config' ).load( 'qa' );
Load particular configs set via NODE_ENV:
// justta
const config = require( './config' );
// ...
When last code is invoked like $ NODE_ENV=production node server.js
, it uses 'production' configs.
Multiple environments can be set as a list separated by commas $ NODE_ENV=production,london-db node server.js
Here, general configs are loaded, followed by the configs in production/
sub-folder and finally the configs from db-london/
sub-folder.
Combining
By default only objects are combined, arrays and simple entities are being overridden. This behaviour can be redefined via special property _combine. It's often convenient to merge arrays instead of replace. For example when you have somewhere in the general configs:
module.exports = {
...
emails: [
'alex@somehost.net',
'ann@somehost.net'
],
};
and in the dev-team
sub-folder
module.exports = {
...
emails: [
'josh@somehost.net',
'helen@somehost.net',
'nathan@somehost.net'
],
_combine: {
emails: true
}
};
Resulting emails
parameter will be merged to
module.exports = {
...
emails: [
'alex@somehost.net',
'ann@somehost.net',
'josh@somehost.net',
'helen@somehost.net',
'nathan@somehost.net'
],
};
instead of just to be overridden by the dev-team
specialization.
Examples
Look into test
folder