npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cconfig

v2.0.2

Published

simple cascading config system

Downloads

43

Readme

Build Status

cconfig

cconfig is a simple cascading config system. It provides a simple, but easily overrideable, solution for environment specific application config.

cconfig loads a config.js or config.json file from your process.cwd(). If it finds that object, it will look for a property in it matching process.env.NODE_ENV (if undefined, it will use NODE_ENV=development) and overwrite any environment-specific properties found there. Then, it will overwrite that object using process.env.

With cconfig you can define global and environment specific config values, and override them using command line level or system level environment variables.

Installation

    npm install cconfig

Setup and usage

    NODE_ENV=development node myscript.js

cconfig uses the NODE_ENV env var to provide environmenet-specific configuration settings. Available settings include process environment variables provided to the process, as well as global and environment-specific settings specified in a config.json file.

config.js || config.json

cconfig can be loaded to expect a config.json or config.js file in the process' base directory cwd:

    var config = require('cconfig')(); 

or you may specify a config file location (this is useful when your application is running in a different directory than where the config.js/config.json resides):

    var config = require('cconfig')('./path/to/my/config.json'); 
    // ----- OR -----
    var config = require('cconfig')('./path/to/my/config.js')

alternatively, you may chose to provide an object as a base configuration

    var defaultConfig = { port: 1337 }
    var config = require('cconfig')(defaultConfig)

Cascading Overrides

The configuration provided through a file or object may include global values and values particular to any NODE_ENV environment name that may be specified. Global variables specified in config.json will override any process environment variable values, and environment-specific items in config.json will override default values defined there as well.

    {
      "MULTI_ENV_VAR": "this var will be used in all environments",
      "OVERRIDEABLE_VAR": "this var will be used, unless overriden for another environment",
      "development": {
        "ANOTHER_VAR": "mongodb://somevalue:27017/somedb"
      },
      "staging": {
        "ANOTHER_VAR": "mongodb://somevalue2:27017/somedb,
        "OVERRIDEABLE_VAR": "this var will be used for staging, unless overriden for another environment",
      },
      "production": {
        "ANOTHER_VAR": "mongodb://somevalue3:27017/somedb,
        "OVERRIDEABLE_VAR": "this var will be used for production, unless overriden for another environment",
      }
    }

usage is simple:

assuming node myApp.js or NODE_ENV=development node myApp.js:

    var config = require('cconfig')();
    
    console.log(config.NODE_ENV); // prints "development"
    console.log(config.MULTI_ENV_VAR"); // prints "this var will be used in all environments"
    console.log(config.OVERRIDEABLE_VAR"); // prints "this var will be used, unless overriden for another environment"
    console.log(config.ANOTHER_VAR"); // prints "mongodb://somevalue:27017/somedb"

assuming NODE_ENV=staging node myApp.js:

    var config = require('cconfig')();
    
    console.log(config.NODE_ENV); // prints "staging"
    console.log(config.MULTI_ENV_VAR"); // prints "this var will be used in all environments"
    console.log(config.OVERRIDEABLE_VAR"); // prints "this var will be used, unless overriden for another environment"
    console.log(config.ANOTHER_VAR"); // prints "mongodb://somevalue2:27017/somedb"

Individual values can be overriden by environment variables specified globally or inline as well.

assuming: OVERRIDEABLE_VAR="text straight from the command line" NODE_ENV=production node myApp.js:

    var config = require('cconfig')();
    
    console.log(config.NODE_ENV); // prints "production"
    console.log(config.MULTI_ENV_VAR"); // prints "this var will be used in all environments"
    console.log(config.OVERRIDEABLE_VAR"); // text straight from the command line"
    console.log(config.ANOTHER_VAR"); // prints "mongodb://somevalue3:27017/somedb"

It's possible to override nested properties from the command line as well. Assuming a config.js file similar to the following:

module.exports = {
    BROKER: {
        HOST: 'localhost'
        PORT: 1243
    }
}

The PORT property can be overriden from the command line via: "BROKER.PORT"=1235 node myApp.js. If you're coding on Windows and want to be able to use similar behavior, see the very useful package cross-env.

Run the tests

    npm test