Robust configuration module for nodejs, built on top of convict
.
npm install rob-config --save
config
is a very simple and powerfull config package but it lacks from schema and validation.
convict
is not simple to use, but way more robust and overridable.
So I wanted something:
- Easy to use like
config
- With schema and documentation like
convict
- With possibility to override with env variable or command line argument like
convict
- With a command to validate config for dev AND before deployment to production
- With a command to see the builded config depends on env and default value (easier to debug)
Here it is and it's very easy to migrate from config
or convict
.
Define a config schema, simple object.
See https://github.com/mozilla/node-convict#the-schema for documentation about schema definition.
For example: config/schema.js
module.exports = {
env: {
doc: 'The applicaton environment.',
format: ['production', 'staging', 'integration', 'development', 'test'],
default: 'development',
env: 'NODE_ENV',
},
api: {
port: {
doc: 'The API port',
format: 'port',
default: 3000,
},
timeout: {
doc: 'The API timeout',
format: 'nat',
default: 60 * 1000, // 1 minutes
},
},
};
Then, define the first config file.
For example: config/development.js
module.exports = {
api: {
port: 3001, // test with "nothing" value to see validation error
},
};
You can, optionnaly, define a config/formats.js
to add one or more custom format to convict.
See https://github.com/mozilla/node-convict#custom-format-checking for documentation about custom formats.
convict.addFormats()
will be call under the hood.
For exemple: config/formats.js
module.exports = {
'float-percent': {
validate: function (val) {
if (val !== 0 && (!val || val > 1 || val < 0)) {
throw new Error('must be a float between 0 and 1, inclusive');
}
},
coerce: function (val) {
return parseFloat(val, 10);
},
},
};
Run ./node_modules/.bin/rob-config show
:
Run ./node_modules/.bin/rob-config validate
:
In case of error:
Run ./node_modules/.bin/rob-config describe
:
const config = require('rob-config');
console.log(config.get('api.port'));
You can set env variable ROB_CONFIG_DIR
with the relative path of your project configuration directory.
To keep better organization of releases we follow the Semantic Versioning 2.0.0 guidelines.
See Releases for detailed changelog.