What is config-chain?
The config-chain package is a utility for managing hierarchical configuration data in Node.js applications. It allows you to load and merge configuration from various sources such as command-line arguments, environment variables, and configuration files. It is useful for applications that need to prioritize configurations from different sources or that need to provide a flexible configuration setup.
What are config-chain's main functionalities?
Loading and merging configurations from multiple sources
This feature allows you to create a new configuration chain and add multiple configuration sources to it. The configurations are merged, with later additions taking precedence over earlier ones. In this example, environment variables and command-line arguments are also included in the configuration chain.
{"ConfigChain": require('config-chain').ConfigChain, "cc": new ConfigChain().add({foo: 'bar'}).add({foo: 'baz', blerg: 'fluff'}).addEnv().addArg() }
Accessing configuration values
Once the configuration chain is set up, you can access configuration values using the 'get' method. This example shows how to retrieve the value of the 'foo' key from the configuration chain.
{"ConfigChain": require('config-chain').ConfigChain, "cc": new ConfigChain().add({foo: 'bar'}), "fooValue": cc.get('foo') }
Using configuration files
Config-chain supports loading configuration from JSON files. You can specify file paths to load and merge configurations from those files. In this example, two configuration files are added to the chain along with environment variables and command-line arguments.
{"ConfigChain": require('config-chain').ConfigChain, "cc": new ConfigChain().addFile('config.json').addFile('/etc/appconfig.json').addEnv().addArg() }
Other packages similar to config-chain
rc
The 'rc' package is similar to config-chain and is used for configuration management. It also loads configuration from command-line arguments, environment variables, and configuration files. However, 'rc' has a simpler API and does not provide the same level of fine-grained control over the order of precedence as config-chain does.
nconf
The 'nconf' package is another alternative to config-chain. It provides a hierarchical configuration with support for files, environment variables, command-line arguments, and atomic object merging. Nconf also includes additional features such as key-value store for configuration data and the ability to make configuration read-only.
convict
Convict is a configuration management library that includes schema-based validation. It allows you to define a schema for your configuration and ensures that the configuration adheres to this schema. Convict can load configurations from JSON files, environment variables, and command-line arguments, similar to config-chain, but with the added benefit of validation.
config-chain
A module for loading custom configurations
NOTE: Feature Freeze

This module is frozen.
In general, we recommend using rc instead,
but as npm depends on this, it cannot be changed.
Install
yarn add config-chain
npm install --save config-chain
Usage
const cc = require('config-chain');
console.log(cc.env('TERM_', process.env));
The .env
function gets all the keys on the provided object which are
prefixed by the specified prefix, removes the prefix, and puts the values on a new object.
Full Usage
var cc = require('config-chain')
, opts = require('optimist').argv
, env = opts.env || process.env.YOUR_APP_ENV || 'dev'
var conf =
cc(
opts,
cc.env('myApp_'),
path.join(__dirname, 'config.' + env + '.json'),
env === 'prod'
? path.join(__dirname, 'special.json')
: null
path.join(__dirname, 'config', env, 'config.json'),
cc.find('config.json'),
{
host: 'localhost'
port: 8000
})
var host = conf.get('host')
var host = conf.store.host
Finally, flexible configurations! 👌
Custom Configuations
var cc = require('config-chain')
var config = cc({
some: 'object'
},
cc.find('config.json'),
cc.env('myApp_')
)
.addUrl('http://configurator:1234/my-configs')
.addFile('/path/to/file.json')
.add({ another: 'object' })
.on('error', function (er) {
throw er
})
.on('load', function (config) {
console.awesome('HOLY SHIT!')
})
API Docs
cc(...args)
MAKE A CHAIN AND ADD ALL THE ARGS.
If the arg is a STRING, then it shall be a JSON FILENAME.
RETURN THE CHAIN!
cc.json(...args)
Join the args into a JSON filename!
SYNC I/O!
cc.find(relativePath)
SEEK the RELATIVE PATH by climbing the TREE OF DIRECTORIES.
RETURN THE FOUND PATH!
SYNC I/O!
cc.parse(content, file, type)
Parse the content string, and guess the type from either the
specified type or the filename.
RETURN THE RESULTING OBJECT!
NO I/O!
cc.env(prefix, env=process.env)
Get all the keys on the provided object which are
prefixed by the specified prefix, removes the prefix, and puts the values on a new object.
RETURN THE RESULTING OBJECT!
NO I/O!
cc.ConfigChain()
The ConfigChain class for CRAY CRAY JQUERY STYLE METHOD CHAINING!
One of these is returned by the main exported function, as well.
It inherits (prototypically) from
ProtoList, and also inherits
(parasitically) from
EventEmitter
It has all the methods from both, and except where noted, they are
unchanged.
LET IT BE KNOWN THAT chain IS AN INSTANCE OF ConfigChain.
chain.sources
A list of all the places where it got stuff. The keys are the names
passed to addFile or addUrl etc, and the value is an object with some
info about the data source.
chain.addFile(filename, type, [name=filename])
Filename is the name of the file. Name is an arbitrary string to be
used later if you desire. Type is either 'ini' or 'json', and will
try to guess intelligently if omitted.
Loaded files can be saved later.
chain.addUrl(url, type, [name=url])
Same as the filename thing, but with a url.
Can't be saved later.
chain.addEnv(prefix, env, [name='env'])
Add all the keys from the env object that start with the prefix.
chain.addString(data, file, type, [name])
Parse the string and add it to the set. (Mainly used internally.)
chain.add(object, [name])
Add the object to the set.
chain.root {Object}
The root from which all the other config objects in the set descend
prototypically.
Put your defaults here.
chain.set(key, value, name)
Set the key to the value on the named config object. If name is
unset, then set it on the first config object in the set. (That is,
the one with the highest priority, which was added first.)
chain.get(key, [name])
Get the key from the named config object explicitly, or from the
resolved configs if not specified.
chain.save(name, type)
Write the named config object back to its origin.
Currently only supported for env and file config types.
For files, encode the data according to the type.
chain.on('save', function () {})
When one or more files are saved, emits save
event when they're all
saved.
chain.on('load', function (chain) {})
When the config chain has loaded all the specified files and urls and
such, the 'load' event fires.