Featureful configuration management library for Node.js (nested structure, schema validation, etc.)
npm install @kauabunga/convict




Convict expands on the standard pattern of configuring node.js applications in a way that is more robust and accessible to collaborators, who may have less interest in digging through imperative code in order to inspect or modify settings. By introducing a configuration schema, convict gives project collaborators more context on each setting and enables validation and early failures for when configuration goes wrong.
* Loading and merging: configurations are loaded from disk or inline and
merged
* Nested structure: keys and values can be organized in a tree structure
* Environmental variables: values can be derived from environmental
variables
* Command-line arguments: values can also be derived from command-line
arguments
* Validation: configurations are validated against your schema (presence
checking, type checking, custom checking), generating an error report with
all errors that are found
* Comments allowed: Schema and configuration files can be either in the
JSON format or in the newer JSON5
format, so comments are welcome
``shell`
npm install convict
An example config.js file:
`javascript
var convict = require('convict');
// Define a schema
var config = convict({
env: {
doc: "The application environment.",
format: ["production", "development", "test"],
default: "development",
env: "NODE_ENV"
},
ip: {
doc: "The IP address to bind.",
format: "ipaddress",
default: "127.0.0.1",
env: "IP_ADDRESS",
},
port: {
doc: "The port to bind.",
format: "port",
default: 8080,
env: "PORT",
arg: "port"
},
db: {
host: {
doc: "Database host name/IP",
format: '*',
default: 'server1.dev.test'
},
name: {
doc: "Database name",
format: String,
default: 'users'
}
}
});
// Load environment dependent configuration
var env = config.get('env');
config.loadFile('./config/' + env + '.json');
// Perform validation
config.validate({allowed: 'strict'});
module.exports = config;
`
An example server.js file leveraging the config.js file above:
`javascript
var http = require('http');
var config = require('./config.js');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
// Consume
server.listen(config.get('port'), config.get('ip'), function(x) {
var addy = server.address();
console.log('running on http://' + addy.address + ":" + addy.port);
});
`
To launch your example server, and set a port:
`shell`
node ./server.js --port 8080
Note: arguments must be supplied with the double-hyphen --arg. (Single hypen's are not supported at this time)
A configuration module, with its deep nested schema, could look like this:
config.js:
`javascript
var config = convict({
db: {
name: {
format: String,
default: ''
},
synchro: {
active: {
format: 'Boolean',
default: false
},
remote_url: {
format: 'url',
default: 'http://localhost:8080/'
}
}
},
secret: {
doc: 'Secret used for session cookies and CSRF tokens',
format: '*',
default: '',
sensitive: true
}
});
config.loadFile(['./prod.json', './config.json']);
`
Each setting in the schema has the following possible properties, each aiding in
convict's goal of being more robust and collaborator friendly.
* Type information: the format property specifies either a built-in convict format (ipaddress, port, int, etc.), or it can be a function to check a custom format. During validation, if a format check fails it will be added to the error report.env
Default values: Every setting must* have a default value.
* Environmental variables: If the variable specified by has a value, it will overwrite the setting's default value. An environment variable may not be mapped to more than one setting.arg
* Command-line arguments: If the command-line argument specified by is supplied, it will overwrite the setting's default value or the value derived from env.doc
* Documentation: The property is pretty self-explanatory. The nice part about having it in the schema rather than as a comment is that we can call config.getSchemaString() and have it displayed in the output.sensitive
* Sensitive values and secrets: If is set to true, this value will be masked to "[Sensitive]" when config.toString() is called. This helps avoid disclosing secret keys when printing configuration at application start for debugging purposes.
In order to help detect misconfigurations, convict allows you to define a format for each setting. By default, convict checks if the value of the property has the same type (according to Object.prototype.toString.call) as the default value specified in the schema. You can define a custom format checking function in the schema by setting the format property.
convict provides several predefined formats for validation that you can use (using node-validator and moment.js). Most of them are self-explanatory:
- any value is validint
* port
* windows_named_pipe
* port_or_windows_named_pipe
* url
*
* ipaddress
* - IPv4 and IPv6 addressesduration
* - milliseconds or a human readable string (e.g. 3000, "5 days")timestamp
* - Unix timestamps or date strings recognized by moment.jsnat
* - positive integer (natural number)
If format is set to one of the built-in JavaScript constructors, Object, Array, String, Number, RegExp, or Boolean, validation will use Object.prototype.toString.call to check that the setting is the proper type.
#### Custom format checking
You can specify a custom format checking method on a property basis.
For example:
`javascript`
var config = convict({
key: {
doc: "API key",
format: function check (val) {
if (!/^[a-fA-F0-9]{64}$/.test(val)) {
throw new Error('must be a 64 character hex key')
}
},
default: '3cec609c9bc601c047af917a544645c50caf8cd606806b4e0a23312441014deb'
}
});
Or, you can use convict.addFormat() to register a custom format checking
method that can be reused for many different properties:
`javascript
convict.addFormat({
name: '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);
}
});
var config = convict({
space_used: {
format: 'float-percent',
default: 0.5
},
success_rate: {
format: 'float-percent',
default: 60.0
}
});
`
The coerce function is optional.
Convict will automatically coerce environmental variables from strings to their proper types when importing them. For instance, values with the format int, nat, port, or Number will become numbers after a straight forward parseInt or parseFloat. duration and timestamp are also parse and converted into numbers, though they utilize moment.js for date parsing.
When merging configuration values from different sources, Convict follows precedence rules. The order, from lowest to highest, is:
1. Default value
2. File (config.loadFile())env
3. Environment variables (only if property is set)arg
4. Command line arguments (only if property is set)config.set()
5. Set and load calls ( and config.load())
convict() takes a schema object or a path to a schema JSON file and returns aJSON5
convict configuration object.
JSON files are loaded using , so they can contain comments.
The configuration object has an API for getting and setting values, described
below.
`javascript
var config = convict({
env: {
doc: "The applicaton environment.",
format: ["production", "development", "test"],
default: "development",
env: "NODE_ENV"
},
log_file_path: {
"doc": "Log file path",
"format": String,
"default": "/tmp/app.log"
}
});
// or
config = convict('/some/path/to/a/config-schema.json');
`
Adds a new custom format.
Adds new custom formats.
Returns the current value of the name property. name can use dot notation to reference nested values. E.g.:`javascript
config.get('db.host');
// or
config.get('db').host;
`
Returns the default value of the name property. name can use dot notation to reference nested values. E.g.:`javascript`
config.default('server.port');
Resets a property to its default value as defined in the schema. E.g.:
`javascript`
config.reset('server.port');
Returns true if the property name is defined, or false otherwise. E.g.:`javascript`
if (config.has('some.property')) {
// Do something
}
Sets the value of name to value. name can use dot notation to reference"db.port"
nested values, e.g. . If objects in the chain don't yet exist,`
they will be initialized to empty objects.
E.g.:javascript`
config.set('property.that.may.not.exist.yet', 'some value');
config.get('property.that.may.not.exist.yet');
// Returns "some value"
Loads and merges a JavaScript object into config. E.g.:`javascript`
config.load({
"env": "test",
"ip": "127.0.0.1",
"port": 80
});$3
Loads and merges one or multiple JSON configuration files into config.JSON5
JSON files are loaded using , so they can contain comments.`
E.g.:javascript`
config.loadFile('./config/' + conf.get('env') + '.json');
Or, loading multiple files at once:
`javascript`
// CONFIG_FILES=/path/to/production.json,/path/to/secrets.json,/path/to/sitespecific.json
config.loadFile(process.env.CONFIG_FILES.split(','));$3
Validates config against the schema used to initialize it. All errors are
collected and thrown or displayed at once.
#### allowed option
1. warn: If set to warn (that is {allowed: 'warn'} is passed), any
properties specified in config files that are not declared in the schema will
print a warning. This is the default behavior.
2. strict: If set to strict (that is {allowed: 'strict'} is passed), any
properties specified in config files that are not declared in the schema will
throw errors. This is to ensure that the schema and the config files are in
sync.
Exports all the properties (that is the keys and their current values) as JSON.
Exports all the properties (that is the keys and their current values) as a JSON
string, with sensitive values masked. Sensitive values are masked even if they
aren't set, to avoid revealing any information.
Exports the schema as JSON.
Exports the schema as a JSON string.
The philosophy was to have production values be the default values. Usually you only want to change defaults for deploy or instance (in aws speak) specific tweaks. However, you can set a default value to null and if your format doesn't accept null it will throw an error.
Thanks to browserify, convict can be used for web applications too. To do so,
* Use brfs to ensure the fs.loadFileSync schema-loading calls are inlined at build time rather than resolved at runtime (in Gulp, add .transform(brfs) to your browserify pipe).http://foo.bar/some.json
To support "loading configuration from a URL"*, build a thin wrapper around convict using your favorite http package (e.g. superagent). Typically, in the success callback, call convict's load()` on the body of the response.
Read the Contributing doc.