dull-config
Configuration should be dull. Less abracadabra.
node.js 4x and up. Not sure about browsers
Philosophy
After looking around at the truly awesome nconf, convict, node-config I decided I wanted to try something simpler.
Here are the goals
- Plain JavaScript Objects, both in and out.
- Keep different environment setting in the same file
- Load secret things from the environment i.e.
process.env.PASSWORD
- No magic searching for config files or file glob loading. Just load files with require()
- Protect from typos and name changes by throwing when accessing an unknown config
This lets you compute your configs or load them from the network as JSON. You use process.env or command line args to set specific configs. You can put your config all in one file or in many files or in the startup file. Its up to you. You can even use validator or joi to validate your config since what you get out are plan JS objects.
That's really it. Its not even that much code 125 loc with docs :-) Not including dependancies.
Installation
$ npm install dull-config
Usage
const config = require('config')
I
config.load({
email : {
defaultTo : 'to@test.com',
defaultFrom : 'from@test.com'
}
})
let to = config.get('email.defaultTo')
config.load({
api : {
url : 'some.place.com',
version : 1
}
})
let to = config.get('email.defaultFrom')
let url = config.get('api.url')
let apiConfig = config.get('api')
apiConfig = config.get('some.typo')
apiConfig = config.has('some.typo')
config.load({
db : require ('./config/db.js'),
stripe : require('./config/stripe.js')
})
Handling Deployment Environment Config differences
{
api : 'some.endpoint.com',
customerPath : '/customer'
}
{
default : {
api : 'some.endpoint.com',
customerPath : '/customer'
},
production : {
api : 'production.endpoint.com'
}
}
{
api : 'production.endpoint.com'
customerPath : '/customer'
}
Local Config Overrides
Sometimes its nice to be able to load a local config file, that overrides settings for this particular local machine. This is useful for testing or old school deployments when you are running on an actual server somewhere. dull-config as a loadLocal() method to let you do this.
Important! It's good practice to NOT check local config into your source control. If you want to remind your self of what goes in there, then check in an example. i.e. local.config.example.js
config/local.js
{
api: local.endpoint.com
}
then in your setup after you've loaded everything else. Try to load it using require. If you use try catch it'll silently skip it if it isn't there.
try{
config.loadLocal(require('/config/local.js'))
}catch(e){
}
Secrets
Don't put secret things in directly in the config. Its bad. Instead use Environmental variables, and then bring them into the config using process.env.SECRET
like this
config/db.js
module.export = {
userName : process.env.DB_USER_NAME,
password : process.env.DB_PASSWORD,
}
index.js
const config = require('config')
config.load({
'db': require('./config/db.js'
})
let user = config.get('db.userName')
user==='admin'
API
Doclet link