# cconfig

> simple cascading config system

Latest version **2.0.2** (published 2018-12-21) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install cconfig
pnpm add cconfig
yarn add cconfig
bun add cconfig
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2018-12-21 |
| First published | 2014-04-28 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | mattwalters5@gmail.com |
| Maintainers | mateodelnorte |

## Links

- npm: https://www.npmjs.com/package/cconfig
- Repository: https://github.com/mateodelnorte/cconfig
- Homepage: https://github.com/mateodelnorte/cconfig#readme
- Issues: https://github.com/mateodelnorte/cconfig/issues
- npm.io page: https://npm.io/package/cconfig

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ~4.1.0

## Recent versions

- 2.0.2 (latest) — 2018-12-21
- 2.0.1 — 2018-12-13
- 1.2.4 — 2017-08-14
- 1.2.3 — 2017-08-14
- 1.2.2 — 2017-08-14
- 1.2.1 — 2017-08-12
- 1.2.0 — 2017-08-11
- 1.1.2 — 2017-03-17
- 1.1.1 — 2016-12-21
- 1.0.0 — 2016-12-09
- 0.2.0 — 2016-07-25
- 0.1.1 — 2015-01-27
- 0.0.1 — 2014-08-27
- 0.0.0 — 2014-04-28

## README

[![Build Status](https://travis-ci.org/mateodelnorte/cconfig.svg?branch=master)](https://travis-ci.org/mateodelnorte/cconfig)

# 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](https://www.npmjs.com/package/cross-env).

# Run the tests
```
    npm test
```
[1]: https://www.npmjs.org/package/cconfig

---
_Source: https://npm.io/package/cconfig · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
