A developer-friendly lightweight replacement for the 'config' module that works with custom config directory and pluggable parsers
A developer-friendly lightweight replacement for the
config module that works with custom config directory and pluggable parsers.
Notice of change of ownership: Starting version 3.0.0 this package has changed it's owner and goals. The old version (2.0.3) is still available on npm via
npm install firstname.lastname@example.org and on github.com/ksmithut/configly. Thank you.
$ npm install --save configly
config module is convenient and easy to start with library, but in the same time being focused that much on "easy" it lacks certain features to be a "developer friendly" library.
This package is addressing those issues, while keeping easy of use and feature set on par with the original module.
To simply replace your current
config setup, add following to your files:
var config = ;console;
It will load
.json files from
relative to the current working directory (
It will cache the result, so files will be read only once per process.
Out of the box
configly supports only two formats (
.json), but developers can add their own parsers and support for more formats (e.g.
var config = ;// more parsersvar ini = ;var cson = ;var yaml = ;var properties = ;var json5 = ;// assemble new parsers list// order doesn't matter since they// will be alphabetically sortedvar configObj =;
Or create new instance with new defaults
var configNew = config;// use it as usualvar configObj = ;
You can export newly created instance and reuse it all over your app,
it won't be affected by other instances of the
configly even if it
used in dependencies of your app, or you module is part of the bigger app,
To load config files from a custom directory, just specify it as the first argument.
var config = './etc'; // `require('configly')('etc');` would work the same way
It will load files from the
etc folder relative to the current working directory,
by providing absolute path, you can make sure exact location of the config files,
which is useful to libraries meant to be used within larger applications
and for command line apps that could be invoked from different directories.
var path = ;var config = path;
Or you can set up new directory as default one
configly without custom arguments
from within other files.
// config.jsvar path = ;var configly = ;moduleexports = configly;// app.jsvar config = ;
It is possible to load files from more than one config directory within one application/module.
var path = ;var ini = ;var configly = ;// "inline"var oneConfig =;
Or creating new default
moduleexports = configly;
configly can load config data from custom files (along with the default list),
handling them the same way – search for supported extensions and within specified directory(-ies).
var config =;
Following code will completely replace list of filenames.
var config =;
For use cases where you need to load config files within the app, but augment it with server/environment specific config you can add absolute path filename to the files list.
var config =;
For bigger apps / more complex configs, combination of multiple directories and custom files would provide needed functionality.
var path =configly =package =;moduleexports = configly;
To fully replicate
config's behavior and provide easy way to include static customized config
in your app files, without resorting to
require('../../../config'), you can create virtual node module,
based on the custom config file within your app.
var path =configly =ini =yaml =;// run configly once with inlined modifiers// and have it as node-cached modulemoduleexports =;
Now npm will copy
./config/ files into
node_modules and execute
./config/config.js on first require,
making it's output available for every file of your app, via
var config = require('config').
This way migration of your app from
config module to
configly will be limited to a few extra lines of code,
while providing more functionality and better separation of concerns out of the box.
For more examples check out test directory.
Main differences between
custom-environment-variablesworks via this mechanism).
hasmethods, it always returns pure js (POJO) object.
configly.PARSERS['json'] = json5.parse;.
Configly is licensed under the MIT license.