configly
A simple configuration management module used for node
projects.
Installation
$ npm install configly --save
Usage
var config = require('configly');
Create a directory in the root of your project called config
.
Every .js
and .json
in that directory will be included in the config object
returned.
For example: if you create a file called lib.json
with these contents...
{
"foo": "bar"
}
...the final config object will look like this...
{
lib: {
foo: 'bar'
}
}
If you use a .js
file instead of a .json
file, then you just need to make
sure you put stuff in the module.exports
object. The above example in .js
form...
module.exports = {
lib: {
foo: 'bar
}
};
If you have a multi-word filename, only use dashes and underscores to separate
the words. This is because the filename is converted to camelCase.
For example: if you create a file called user-permissions.json
with these
content...
{
"/": [
"anonymous",
"admin"
],
"/admin": [
"admin"
]
}
...the final config object will look like this...
{
userPermissions: {
'/': [
'anonymous',
'admin'
],
'/admin': [
'admin'
]
}
}
Environment
To signify that a config file is an environment config file, use this naming
convention:
env.[environment name].js(on)
The environment name
should be the name of the environment you want the config
file to be for.
For example, if it's for a development environment, you should name the config
file env.development.json
or env.development.js
.
To change the environment used, you just need to specify the NODE_ENV
variable
when running your app. E.g. NODE_ENV=production node app
If no NODE_ENV
variable is provided, it will default to development
, so you
should always have an env.development.json
file
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.