Config management module. It uses nconf under the bonnet to manage config files, environment variables as a single system. It aims to simplify system configuration and config management. It also offers means of config validation and normalization using json schema.
$ npm i @ahmdigital/config
const config = require('@ahmdigital/config');
module.exports = config.make({ path: `${__dirname}/..` });
There're several options that config.make
takes:
default
string - Path to default config file (defaultdir/default.config.json
).local
string - Path to local config file (defaultdir/local.config.json
).normalise
boolean - Attempt to normalise string literals that come from env (defaulttrue
).onError
function - Error callback function, e.g. when config doesn't match schema (defaultconsole.error
).path
string - Directory to look for json configs.schema
string - Path to json schema file (defaultdir/schema.json
).
See Example.
Configuration is loaded with the following priority:
- Environment variables (process.env) with
__
as the separator. - Local config (usually not checked in to version control).
- Environment specific config
<env>.config.json
. - Default config.
- System defaults.
NODE_ENV
current environment name, e.g.development
orproduction
NODE_CONFIG_DIR
directory where to look for config files, project's base directory by defaultNODE_CONFIG_FILE
custom config file with overridesfoo__bar__baz
to overridefoo: { bar: { baz: value } }
- Use hierarchical configs where possible
- Use json types where it makes sense, avoid type casting later on in your consumer code
- Keep devops specific config values (like elasticache hostname) in infrastructure config
- Keep sensitive values in infrastructure config or/and consider config encryption - nconf can do it