nconf configuration file loader
Loads nconf configuration files from disk.
It looks for config.json in the specified directory. Should config.x.json exist, the configuration will be overloaded with the environment config. Should config.x.local.json exist, the configuration will be overloaded with the local config.
x is defined by NODE_ENV
environment variable (development if not set).
It also handles references to environment variables by prefixing a variable name with "$"
See NCONF for additional info.
Usage
There are two requirements to run the script:
- A wellformed json file named config.json, config.x.json and/or config.x.local.json where x is
NODE_ENV
or development. - Point to the directory location of the config file(s).
Load the configuration
;
Examples
config.json:
"fruit": "pear" "drink": "beer" "food": "spaghetti"
config.development.json:
"fruit": "apple" "drink": "milk"
config.development.local.json:
"fruit": "ananas"
config.production.json:
"fruit": "pineapple" "drink": "pina colada"
config.enve.json:
"fruit": "$FRUIT" "drink": "$DRINK"
app.js:
var config = ; console;console;console;
The result when running node app.js
:
fruit of choice: ananas
favourite drink: milk
food of choice: spaghetti
The result when running NODE_ENV=production node app.js
:
fruit of choice: pineapple
favourite drink: pina colada
food of choice: spaghetti
The result when running NODE_ENV=enve FRUIT=mango DRINK=powderday node app.js
:
fruit of choice: mango
favourite drink: powderday
food of choice: spaghetti
All of the examples above can be found in the examples/
folder.