Socket
Socket
Sign inDemoInstall

config-chain

Package Overview
Dependencies
2
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    config-chain

HANDLE CONFIGURATION ONCE AND FOR ALL


Version published
Weekly downloads
8M
decreased by-4.91%
Maintainers
2
Install size
21.0 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

#config-chain

USE THIS MODULE TO LOAD ALL YOUR CONFIGURATIONS


  //npm install config-chain

  var cc = require('config-chain')
    , opts = require('optimist').argv //ALWAYS USE OPTIMIST FOR COMMAND LINE OPTIONS.
    , env = opts.env || process.env.YOUR_APP_ENV || 'dev' //SET YOUR ENV LIKE THIS.

  // EACH ARG TO CONFIGURATOR IS LOADED INTO CONFIGURATION CHAIN
  // EARLIER ITEMS OVERIDE LATER ITEMS
  // PUTS COMMAND LINE OPTS FIRST, AND DEFAULTS LAST!

  //strings are interpereted as filenames.
  //will be loaded synchronously

  var conf = 
  cc(
    //OVERRIDE SETTINGS WITH COMMAND LINE OPTS
    opts,

    //ENV VARS IF PREFIXED WITH 'myApp_'

    cc.env('myApp'), //myApp_foo = 'like this'

    //FILE NAMED BY ENV
    path.join(__dirname,  'config.' + env + '.json'), 

    //IF `env` is PRODUCTION
    env === 'prod' 
      ? path.join(__dirname, 'special.json') //load a special file
      : null //NULL IS IGNORED!

    //SUBDIR FOR ENV CONFIG
    path.join(__dirname,  'config', env, 'config.json'), 
    
    //SEARCH PARENT DIRECTORIES FROM CURRENT DIR FOR FILE
    cc.find('config.json'),
    
    //PUT DEFAULTS LAST
    { 
      host: 'localhost'
      port: 8000
    })

  var host = conf.get('host')
  
  // or

  var host = conf.store.host

FINALLY, EASY FLEXIBLE CONFIGURATIONS!

##see also: (proto-list)[https://github.com/isaacs/proto-list/]

##TODO

  • add support for more types of parser, yaml, ini, whatever I can find in npm.
  • if string is http://... load config from internet. (useful for devops)

FAQs

Last updated on 05 Aug 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc