config-chain
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
proto-list | 1.2.4 | 2.04 kB | ISC | prod |
Config-chain is a powerful JavaScript module specially designed to handle custom configurations in your applications. Its functionalities enable you to define, process, and manage your configuration setup in a flexible and efficient way. This library is particularly useful for applications that require sophisticated configuration management, offering a variety of options for defining and overriding your settings. Despite being frozen, which means there will be no new features added, config-chain remains stable and highly valuable in instances where npm depends on it.
Utilizing config-chain in your JavaScript code is straightforward, after installing it via npm or yarn. Below is a simplified usage example:
const cc = require('config-chain');
console.log(cc.env('TERM_', process.env));
/*
{ SESSION_ID: 'w1:5F38',
PROGRAM_VERSION: '3.1.2',
PROGRAM: 'iTerm.app' }
*/
Here, the cc.env
function fetches all keys from the given object (process.env in this case), which have a specific prefix (in this case "TERM_"). Next, it removes this prefix and assigns the values to a new object.
For a more elaborate use case, you can manage multiple configuration layers and set precedence, here's how:
var cc = require('config-chain')
var conf =cc(
// Command line options
opts,
// Environment variables with 'myApp_' prefix
cc.env('myApp_'),
// Config file named by environment
path.join(__dirname, 'config.' + env + '.json'),
// If `env` is 'prod', load a special.json file
env === 'prod' ? path.join(__dirname, 'special.json') : null,
// Nested directory config
path.join(__dirname, 'config', env, 'config.json'),
// Search parent directories for a specific file
cc.find('config.json'),
// Defaults
{
host: 'localhost',
port: 8000
}
)
var host = conf.get('host');
The Config-chain package allows you to harness the power of layered configurations to ensure your application adapts seamlessly to various environments and conditions.
The comprehensive documentation for config-chain can be found within the GitHub repository README file, providing an overview of its use, examples showing how to implement it, as well as detailed explanations of its APIs and methods. This great resource will steer you through the process of initializing config-chain, setting and getting configuration values, loading configuration from files, URLs, environment variables and other sources, and handling configuration errors. With these detailed instructions and explanations, you will be well-equipped to leverage the full potential and versatility of the config-chain package in your projects.