# multiconfig

> A recursive, hierarchical, multi-file, environment based configuration loader

Latest version **1.1.6** (published 2015-12-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2015-12-13 |
| First published | 2015-06-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Arjan Frans |
| Maintainers | arjanfrans |
| Keywords | config, configuration, config loader, settings, json config loader, yaml config, cson config, multiconfig |

## Links

- npm: https://www.npmjs.com/package/multiconfig
- Repository: https://github.com/ArjanFrans/node-multiconfig
- Homepage: https://github.com/ArjanFrans/node-multiconfig#readme
- Issues: https://github.com/ArjanFrans/node-multiconfig/issues
- npm.io page: https://npm.io/package/multiconfig

## Dependencies (6)

- [ini](https://npm.io/package/ini.md) ^1.3.4
- [debug](https://npm.io/package/debug.md) ^2.2.0
- [extend](https://npm.io/package/extend.md) ^3.0.0
- [yamljs](https://npm.io/package/yamljs.md) ^0.2.4
- [camel-case](https://npm.io/package/camel-case.md) ^1.2.0
- [cson-parser](https://npm.io/package/cson-parser.md) ^1.3.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2015-12-13
- 1.1.5 — 2015-09-13
- 1.1.4 — 2015-09-12
- 1.1.3 — 2015-09-12
- 1.1.2 — 2015-09-11
- 1.1.1 — 2015-09-10
- 1.1.0 — 2015-09-09
- 1.0.1 — 2015-09-06
- 1.0.0 — 2015-08-24
- 0.1.5 — 2015-08-01
- 0.1.4 — 2015-07-29
- 0.1.3 — 2015-07-28
- 0.1.2 — 2015-07-02
- 0.1.1 — 2015-06-29
- 0.1.0 — 2015-06-29
- … 9 more at https://npm.io/package/multiconfig/versions

## README

# node-multiconfig
[![NPM](https://nodei.co/npm/multiconfig.png)](https://www.npmjs.com/package/multiconfig)

[![Build Status](https://travis-ci.org/arjanfrans/node-multiconfig.svg?branch=master)](https://travis-ci.org/arjanfrans/node-multiconfig)
[![Coverage Status](https://coveralls.io/repos/arjanfrans/node-multiconfig/badge.svg)](https://coveralls.io/r/arjanfrans/node-multiconfig)
[![Dependency Status](https://david-dm.org/arjanfrans/node-multiconfig.svg)](https://david-dm.org/arjanfrans/node-multiconfig)
[![devDependency Status](https://david-dm.org/arjanfrans/node-multiconfig/dev-status.svg)](https://david-dm.org/arjanfrans/node-multiconfig#info=devDependencies)

A recursive, hierarchical, multi-file, environment based configuration loader.
It supports JSON, YAML, INI and CSON.

This module loads multiple .json files recursivly. It support different configurations for different environments
(using the `NODE_ENV` environment variable). Inside configuration files you can specify environment variable placeholders using a prefix. 
All configurations for the given environment will be loaded into an object.

## Installation
Install the module:
```
npm install multiconfig --save
```

## Usage
In order to use this module, configuration files must be structured in a certain way. 
A *config* directory must be present in the root directory of your project.
The *config* directory should have a *default* directory, in which default configurations are stored.
```
mkdir -p config/default
```

In your application configurations are accessible as an object, using the same pattern as the directory structure.
By default, directory and file names are converted to camel case (this can be disabled).

Each directory can contain a file named `index` (which can be any of the support extensions, for example `index.yaml`).
All the values of this file be a property of the directory name. (see example)

Optionally, for each environment (e.g. *development*, *staging* or *production*) another directory is created.
In this directory you can create configurations that will override default configurations.
The supportedd file types can be mixed.
The environment is specified with the `NODE_ENV` environment variable.
```
mkdir config/production
```

## Usage

```Node
var multiconfig = require('multiconfig');

var config = multiconfig.load();

// Do whatever you want with the 'config' object, 
// 'config' is just a regular Javascript object.
```

Optionally you can pass an `options` object as the first argument.

```Node
var options = {
    directory: '/path/to/config/directory', //specify the config directory (can be relative or absolute)
    env: 'staging', // Specify the environment
    prefix: '$ENV_VAR',
    pathCase: false // Don't convert directorie/file names to camelCase.
    configVariation: 1 // Use configs suffixed with "-1", "-2", etc. 
};

var config = multiconfig.load(options);
```

The default `prefix` is `$ENV`. It uses `:` as a separator. An example value in a configuration file: `$ENV:YOUR_ENVIRONMENT_VAR_HERE`. 
The value will be replaced by the actual value of the environment variable, if it exists. Check the example below.

The config variation to use can also be set using `NODE_CONFIG_VARIATION` environemnt variable.
The config directory can also be specified using `NODE_CONFIG_DIRECTORY` an environment variable. Environment variables
will have priority over inline configuration.

## Example

This example uses JSON and YAML. other formats will work in the same way, just a different syntax.
Assume we have an environment variable `TEST_DATA` with the value `awesome`. We have the following configuration files (note the paths):

*./config/default/index.yaml*
```yaml
applicationName: multiconfig
```

*./config/default/database/mongo.json*
```json
{
  "host": "127.0.0.1",
}
```

*./config/default/compilers/java.json*
```json
{
  "version": "7",
  "name": "Java"
}
```


*./config/production/compilers/java.json*
```json
{
  "version": "6",
  "debug": false  
}
```


*./config/production/database/redis-config.json*
```json
{
  "database": "$ENV:TEST_DATA",
  "host": "10.0.0.1",
  "port": 9920
}
```

If an application is run without any environment specified, this will be the output of the configuration loader:
```json
{
  "applicationName": "multiconfig",
  "compilers": {
    "java": {
      "version": "7",
      "name": "Java"
    }
  },
  "database": {
    "mongo": {
      "host": "127.0.0.1"
    }
  }
}
```

Note the camel case conversion of the `redis-config` file.
By default every directory or filename will be converted to camel case.

If we run the application with `NODE_ENV=production` the output will be the following:
```json
{
  "applicationName": "multiconfig",
  "compilers": {
    "java": {
      "version": "6",
      "name": "Java",
      "debug": false
    }
  },
  "database": {
    "mongo": {
      "host": "127.0.0.1"
    },
    "redisConfig": {
      "database": "awesome",
      "host": "10.0.0.1",
      "port": 9920
    }
  }
}
```

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