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 = require'configly';console.logconfigmycombinedoptions;
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 = require'configly';// more parsersvar ini = require'ini';var cson = require'cson';var yaml = require'js-yaml';var properties = require'properties';var json5 = require'json5';// assemble new parsers list// order doesn't matter since they// will be alphabetically sortedconfigPARSERS =ini : iniparse// have it as a wrapper to prevent extra arguments leakingreturn csonparsestr;return yamlsafeLoadstr;// same options as used within `config` modulereturn propertiesparsestr namespaces: true variables: true sections: true;// use json5 instead of `JSON.parse`json : json5parse// keep the original onejs : configPARSERSjs;var configObj = config;
configly is a singleton, this setup could be done in your index file,
and the rest of the files would use it the same way as in the "Basic" example.
To load config files from a custom directory, just specify it as the first argument.
var config = require'configly''./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 = require'path';var config = require'configly'pathjoin__dirname 'etc';
Or you can set up new directory as default one
configly without custom arguments
from within other files.
// index.jsvar path = require'path';var configly = require'configly';configlyDEFAULTSdirectory = pathjoin__dirname 'etc';// app.jsvar config = require'configly';
It is possible to load files from more than one config directory within one application/module.
var path = require'path';var ini = require'ini';var configly = require'configly';var appConfig = configlypathjoin__dirname 'app-config';// for example you have .ini config files therevar rulesConfig = configlypathjoin__dirname 'rules-config' ini: iniparse;
If there is a need to merge standalone config objects into one,
you can use
configly.merge method manually,
in the order that suites your specific use case.
var oneConfig = configlymergeappConfig rulesConfig;
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.