Configure your Node.js Applications
Introduction
my-nconf organizes hierarchical configurations for your app deployments.
It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).
Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables
Install:
$ npm i my-nconf -s
Usage:
config.get()
will throw an exception for undefined keys to help catch typos and missing values.
Use config.has()
to test if a configuration value is defined.
Use configs in your code:
//./app/server.js; var config = ;var dbConfig = config;db; if config var detail = config; //...
By default loadConfig
will load in config files found at callsite
directory (e.g ./app/
)
then it will attempt to read and extendDeep
appropriate config files found at lookuppaths
in order
let lookuppaths = [
{callsite}/config
,{callsite}/.config
,{project dir}
,{project dir}/config/
,{project dir}/.config/
,{cwd}
,{cwd}/config/
,{cwd}/.config/
,/Users/{username}/config/
,/Users/{username}/.config/
,/etc/config
,/etc/.config
,/config
,/.config
,confDirs //e.g loadConfig(confDir) loadConfig([confDir1, dir2, dir3], opts)
,process.env.NODE_CONFIG_DIR
];
Additionally loadConfig
caller's package.name
will be appended after each lookuppaths
Start your app server:
$ export NODE_ENV=production$ node ./app/server.js
Included parsers:
- js-yaml
- properties
- x2js
- json5
- hjson