UnCfg
Just another configuration helper for Node.js.
Features
- Very simple, no tweaking. Give it your
config
folder and start working! - Supports env-specific settings. Make a folder with the env name, set your
NODE_ENV
variable and ready to go! - Supports calculated config, each config it's a normal
.js
file. Readprocess.env
or do Math, you just have to give a value.
Usage
First, add the dependency to your package.json
:
npm install --save uncfg
Then, require()
it in your app, and make use of it:
// Require the packageconst path = // miscellaneous config = ; // Load configuration from `config` directoryconfig; // Read value (both are valid)let port = ;let port = config; // Read nested value (both are valid)let host = ;let host = config; // Read value with default (only with `.get`)let sample = config; // Save value (both are valid);config; // Get copy of storelet cfg = ;console; // worldconsole; // 3000console; // localhost
The code above assumes your app is structured like this:
my-awesome-app+-- app.js+-- package.json+-- config| +-- port.js| +-- database.js| +-- production| | +-- database.js+-- node_modules
If you need more guidance, feel free to check the tests, specifically in the config test folder.
Specific example
So you need even more examples? Ok, let's do it!
// config/database.jsmoduleexports = hostname: 'localhost' database: 'ohmydear' username: 'notroot' password: 123456789; // config/production/database.jsmoduleexports = url: 'mongodb://me:pwd@server.org/test'; // app.jsconst path = config = ; config; let dbCfg = ;console; // { hostname: 'localhost', // database: 'ohmydear', // username: 'notroot', // password: 123456789, // url: 'mongodb://me:pwd@server.org/test' } // *NOTE*: `url` is available if `NODE_ENV=production`. let host = ;let user = ;
License
This project is released under the MIT license.