Configuration-loader utility for
Node.js to load different set of configuration files depending on your execution environment.
Simply prepare your configuration files and call
require('app-config'). Available configurations and environments are determined dynamically, based on your directory and file structures.
In Java environment,
classpath is used to 'inject' paths of right configuration files.
classpath. This tool is here to solve your multi-environment configuration problem.
Create database config file for
Give the config file some content
moduleexports =hostname: 'localhost'port: 1234
Load the config in your app
var config = ;console;
Run your app
NODE_ENV=dev node you_app.js
Like it? Read on for more details.
An example of directory structure required by the tool.
Each environment is a separate directory of the same name. In the example above, we have two environments:
Configuration files are stored within each environment directory. Any sub-directory under an environment directory will be ignored.
Configuration files located directly under
config/ directory are supported for two reasons:
config/directory depending on which environment you are building a package for.
config/prod/into the package's
moduleexports =emitErrors: truefileLogConfig:filename: '/var/log/app.log'level: 'debug'
require('app-config'), configuration files are loaded depending on the execution environment,
which is being set by using environmental variables. More on that below.
Returned object upon
require call for the example directory structure:
log:emitErrors: truefileLogConfig:filename: '/var/log/app.log'level: 'debug'db:...
var config = ;console;
All configuration options are available via the environment variables below. Some examples of how to set the environmental variables:
NODE_ENV=dev NODE_CONFIG_NO_HALT= node index.js
NODE_ENV=qa NODE_CONFIG_DIR='/qa/config/path/' node index.js
NODE_ENV=prod NODE_CONFIG_LOG= NODE_CONFIG_DIR='/prod/config/path/' node index.js
Environment to be used when requiring the tool. In our example, we have
Configuration directory is expected to be called 'config' and to be located in the root of your app/project. You can change this default behavior by setting this option up to whatever path you like.
If an error occurs during the load of config files, the app is halted unless overridden by defining this option to whatever value.
For example, the app is halted if the configuration directory does not exist, if an environment does not exist in
config directory, etc.
The app is being halted by executing
process.exit(-1) command. If the halt is prevented by defining this option,
the returned value by the
require call is
null and it is up to you to handle this situation.
Prints out information and error messages into console if defined.
This tool is fully synchronous. I.e. for all file operations, synchronous Node.js functions are being called.
This is essential to have the config files loaded immediately upon
Since Node.js caches all
require calls, this tool will be executed only ones ensuring no performance impact on the app.
The tool walks through
config/ directory in your app's root (or any other provided directory via configuration) and scans for environment directory.
When found, an object is returned, having properties of the same name as the log files - just the .js extension is stripped out.
Object returned by
require call on our example directory structure above with
db: ;log: ;
Prior requiring the
log modules above, they are deleted from
This will ensure reloading of config files if you intentionally deleted
app-config module from
Useful for unit testing when playing around with properties in object returned upon
npm install app-config
The tool does not depend on any other code. For developers of this tool,
Should are the only dependencies for running unit tests.
In order to develop this plugin, these steps are required:
git clone URL
npm installto download dependencies