# dynamic-config

> Loads configuration files depending on the given env

Latest version **1.0.0** (published 2016-06-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install dynamic-config
pnpm add dynamic-config
yarn add dynamic-config
bun add dynamic-config
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-06-23 |
| First published | 2014-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | peerigon |
| Maintainers | peerigon |
| Keywords | config, env, args |

## Links

- npm: https://www.npmjs.com/package/dynamic-config
- Repository: https://github.com/peerigon/dynamic-config
- Homepage: https://github.com/peerigon/dynamic-config#readme
- Issues: https://github.com/peerigon/dynamic-config/issues
- npm.io page: https://npm.io/package/dynamic-config

## Dependencies (3)

- [minimist](https://npm.io/package/minimist.md) ^1.2.0
- [deep-assign](https://npm.io/package/deep-assign.md) ^2.0.0
- [alamid-plugin](https://npm.io/package/alamid-plugin.md) ^0.1.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-06-23
- 0.3.1 — 2016-06-01
- 0.3.0 — 2016-06-01
- 0.2.0 — 2016-04-04
- 0.1.4 — 2015-08-26
- 0.1.3 — 2015-02-23
- 0.1.2 — 2014-11-20
- 0.1.1 — 2014-10-29
- 0.1.0 — 2014-10-16
- 0.0.3 — 2014-10-01
- 0.0.2 — 2014-05-15
- 0.0.1 — 2014-04-03

## README

dynamic-config
==============
**Dynamic configuration files**

[![](https://img.shields.io/npm/v/dynamic-config.svg)](https://www.npmjs.com/package/dynamic-config)
[![](https://img.shields.io/npm/dm/dynamic-config.svg)](https://www.npmjs.com/package/dynamic-config)
[![Dependency Status](https://david-dm.org/peerigon/dynamic-config.svg)](https://david-dm.org/peerigon/dynamic-config)
[![Build Status](https://travis-ci.org/peerigon/dynamic-config.svg?branch=master)](https://travis-ci.org/peerigon/dynamic-config)
[![Coverage Status](https://img.shields.io/coveralls/peerigon/dynamic-config.svg)](https://coveralls.io/r/peerigon/dynamic-config?branch=master)

Loads configuration files depending on:

  - argv: `node app.js --env production`
  - env: `export NODE_ENV=production; node app.js`

Expects a `.js` file as config so you can add dynamic content.

## Installation

```
npm install dynamic-config --save
```

## Options

### defaultEnv: string = *"develop"*

Define which env should be set as default.

### log: boolean = *false*

Enable logging of path/env resolution.

### envName: string = *"env"*

The argument/env variable name we expect.

## Example

```javascript
// config/index.js

const DynamicConfig = require("dynamic-config");
const dynamicConfig = new DynamicConfig({
    defaultEnv: "develop",
    log: true
});

module.exports = dynamicConfig.load(__dirname, "config.js");
```

```javascript
// config/develop/config.js

module.exports = {
    whereami: "develop"
}
```

```javascript
// app.js
const config = require("./config");

console.log(config);
```

```javascript
node app.js

{ whereami: 'develop' }

// Set environment via args
node app.js --env prod

{ whereami: 'prod' }

// Set environment via env
export env=stage; node app.js

{ whereami: 'stage' }
```





## Plugins

### extend

These plugins allow you to override specific config fields by applying them via env, argv or a separate local config file.

```javascript
const dynamicConfig = new (require("dynamic-config"))();

// extend from env
dynamicConfig.use(require("dynamic-config/plugins/extend/env"));

// extend from file
dynamicConfig.use(require("dynamic-config/plugins/extend/file"));

// extend from argv
dynamicConfig.use(require("dynamic-config/plugins/extend/argv"));

module.exports = dynamicConfig.load(__dirname, "config.js");
```

**Hint:** The order in which the plugins are applied is important. In the above code snippet, config fields defined via arguments would override their counterparts from an override file, which itself overrides fields from environment variables. This is probably a suitable order for most projects.

```javascript
node app.js

{ name: 'superApp', port: 9000 }

// Overwrite via argv
node app.js --port 80
// ... or ...
node app.js --port=80

{ name: 'superApp', port: 80 }

// Overwrite via env
export port=90 node app.js

{ name: 'superApp', port: 90 }

// Order matters...
export port=90; node app.js --port 80

{ name: 'superApp', port: 80 }
```

#### Extend via file
Create a file named the same as your config, but contains `.local` in front of the extension, like `config.js` becomes `config.local.js`.

In the config extension file you can define any subset of the object, that is defined in the main config and it would overwrite the corresponding value. Both configs are merged via [deep-assign](https://github.com/sindresorhus/deep-assign).

```javascript
// config.js
module.exports = {
    a: 1,
    b: {
        c: "c",
        d: 2
    },
    e: 3
}

// config.local.js
module.exports = {
    e: "e",
    b: {
        d: "d"
    }
}

// result
{
    a: 1,
    b: {
        c: "c",
        d: "d"
    },
    e: "e"
}
```

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