configly
![Coverage Status](https://coveralls.io/repos/ksmithut/configly/badge.png?branch=develop)
A simple configuration management module used for node.js
projects.
Installation
$ npm install configly --save
Usage
-
After installing the configly
package (see above), Create a directory in
the root of your project called config
.
-
Put configuration files in your newly created config
directory
- Filenames should be all lowercase letters with words separated by
-
- The configuration files should be a
.json
file or .js
file .js
files should module.exports =
the config object- Environment config files should be named as such:
env.[environmentName].json
or env.[environmentName].js
. - The default environment is
development
. - To use a different environment config file, start your node app like this:
NODE_ENV=production node app
replacing production
with the environment you wish be in.
-
Include the config object in your files:
var config = require('configly');
The config object return will reflect the data put into your configuration
files.
Each of the files is a property attached to the config
object. The
property name is a camelCased version of the name (e.g. filename =
foo-bar.json
, config = { fooBar: ... }
see example below).
The environment config gets put in as the env
property.
Example
Imagine a directory structure like this:
project/
├─ config/
│ ├─ env.development.json
│ ├─ env.production.json
│ ├─ user-permissions.json
│ └─ email.js
├─ node_modules/
│ └─ configly/...
├─ package.json
└─ app.js
config/env.development.json
{
"port": "3000",
"cachAge": 0
}
config/env.production.json
{
"port": "80",
"cacheAge": 86000
}
config/user-permissions.json
{
"/": [
"admin",
"anonymous"
],
"/admin": [
"admin"
]
}
config/email.js
'use strict';
var emailConfig = {};
emailConfig.user = 'email@email.com';
emailConfig.password = 'my super secure password';
module.exports = emailConfig;
app.js
'use strict';
var config = require('configly');
console.log(config);
Alright, now with that setup, we run this command:
$ node app
We get this output:
{ email:
{ user: 'email@email.com',
password: 'my super secure password' },
env: { port: '3000', cachAge: 0 },
userPermissions: { '/': [ 'admin', 'anonymous' ], '/admin': [ 'admin' ] } }
But when we run this command:
$ NODE_ENV=production node app
We get this ouput:
{ email:
{ user: 'email@email.com',
password: 'my super secure password' },
env: { port: '80', cacheAge: 86000 },
userPermissions: { '/': [ 'admin', 'anonymous' ], '/admin': [ 'admin' ] } }
Notice the only change was in the environment variable. I don't know about you,
but this is super handy, because now deployment becomes a breeze.
Any file you add to the config
directory will automatically be added to the
config object. No need to include it in some master config file.
Also, no 3rd party dependencies. The only core dependencies it has are fs
and
path
.
Troubleshooting
If there is some behavior that isn't expected, like the config object isn't
in the format you expected, try console.log
on the config object.
If there is no config
directory in what is considered your 'current working
directory' (this can be found by process.cwd()
), then the config object will
be a javascript Error
object.
If there was an error parsing the .json
or .js
, then the property that it
was supposed to be in will be a SyntaxError
object.
Any other issues, please report to this repo's issues on GitHub.
Caveats
Although you can a config file with the same name but have different extentions
(i.e. .js
and .json
), you shouldn't because one of them will not be
included. From the tests that I've done, it takes the .json
version because
it shows up later in the list. At any rate, it seems like it would be bad
practice to have two files with the same name in the same directory.
Your config directory has to be where your process.cwd()
resides. In the
future, I would like this to be configurable, but in the spirit of quick
iterations and getting feedback, I will save that for another day.