# ms-conf

> wrapper over dotenv and nconf module for one line configuration loading

Latest version **8.2.1** (published 2023-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install ms-conf
pnpm add ms-conf
yarn add ms-conf
bun add ms-conf
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.2.1 |
| Published | 2023-06-26 |
| First published | 2016-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 10 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Vitaly Aminev |
| Maintainers | avvs |
| Keywords | configuration, nconf, env, docker |

## Links

- npm: https://www.npmjs.com/package/ms-conf
- Repository: https://github.com/makeomatic/ms-conf
- Homepage: https://github.com/makeomatic/ms-conf#readme
- Issues: https://github.com/makeomatic/ms-conf/issues
- npm.io page: https://npm.io/package/ms-conf

## Dependencies (10)

- [glob](https://npm.io/package/glob.md) ^10.3.0
- [debug](https://npm.io/package/debug.md) ^4.1.0
- [nconf](https://npm.io/package/nconf.md) ^0.12.0
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.3
- [camelcase](https://npm.io/package/camelcase.md) ^6.0.0
- [eventemitter3](https://npm.io/package/eventemitter3.md) ^5.0.1
- [secure-json-parse](https://npm.io/package/secure-json-parse.md) ^2.7.0
- [@fastify/deepmerge](https://npm.io/package/@fastify/deepmerge.md) ^1.3.0
- [synchronous-worker](https://npm.io/package/synchronous-worker.md) ^1.0.5
- [@makeomatic/confidence](https://npm.io/package/@makeomatic/confidence.md) 6.0.3

## 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

- 8.2.1 (latest) — 2023-06-26
- 8.2.0 — 2023-06-22
- 8.1.2 — 2023-05-17
- 8.1.1 — 2023-05-17
- 8.1.0 — 2023-05-09
- 8.0.2 — 2023-05-08
- 8.0.1 — 2023-05-08
- 8.0.0 — 2023-05-08
- 7.0.2 — 2020-09-14
- 7.0.1 — 2020-09-14
- 7.0.0 — 2020-04-23
- 6.0.1 — 2020-03-22
- 6.0.0 — 2020-01-21
- 5.0.2 — 2019-08-29
- 5.0.1 — 2019-08-21
- … 21 more at https://npm.io/package/ms-conf/versions

## README

# Configuration loading module

Combines confidence, dotenv, nconf and processes env configuration into camelcase;
Each value is JSON parsed first if it is possible, therefore you can pass arrays/objects and boolean params through env

## Installation

`npm i ms-conf` or any other manager

## Configuration

Accepts following configuration params, which can be passed through `.env` or process.env

1. `DOTENV_NOT_SILENT` - if present, warnings from dotenv wont be supressed
2. `DOTENV_ENCODING` - defaults to 'utf-8'
3. `DOTENV_FILE_PATH` - if .env file isn't located in the root of your module, you can pass path here
4. `NCONF_SEPARATOR` - defaults to `__`
5. `NCONF_MATCH` - only envs, matched by this would be returned through configuration
6. `NCONF_MATCH_OPTS` - opts for regexp constructed from `NCONF_MATCH`
7. `NCONF_WHITELIST` - stringified JSON array of variebles that should be parsed into final configuration
8. `NCONF_FILE_PATH` - external JSON configuration file that will be used to provide variables
9. `NCONF_NAMESPACE` - **MUST** be specified, as it will return configuration relative to this namespace
10. `NCONF_NO_CAMELCASE` - if present, keys will not be camelCased

## Usage

`.env` file:

```
NCONF_NAMESPACE=MS_CONF
NCONF_MATCH=^MS_CONF
NCONF_MATCH_OPTS=i
MS_CONF__AMQP__HOSTS=["127.0.0.1"]
MS_CONF__AMQP__SSL=true
MS_CONF__AMQP__STRING_TRUE='"true"'
NCONF_FILE_PATH=["/etc/app-configs","/etc/nice-config.js","/opt/app/bundle.json"]
```

```js
// ESM
import { Store } from 'ms-conf';
// CJS
const { Store } = require('ms-conf');

// get basic configuration
const store = new Store()
const config = store.get('/');
// config would equal
// {
//   amqp: {
//     hosts: ['127.0.0.1'],
//     ssl: true,
//     stringTrue: 'true'
//   }
// }

store.get('/amqp') // will return inner object and so on
store.get('/amqp/hosts') // ['127.0.0.1']
```

## Hot Reload

```js
store.enableReload();
// send SIGUSR1 or SIGUSR2 signal to process to reload configuration

store.disableReload();
// wont listen for SIGUSR1 or SIGUSR2 events any longer
```

## Utilities

1. Load file path

```js
// CJS
const { globFiles } = require('ms-conf');
// ESM
import { globFiles } from 'ms-conf';

// generates config for the passed files bypassing public API
const config = globFiles(
  undefined, // prependFile: string | undefined,
  ['/path/to/configs', '/path/to/config/direct.js', '/path/to/conf.json'], // fileList: string | string[]
  {}, // config: baseConfig
  true, // throw error in case of file processing issues
);
```

2. setDefaultOpts

```js
// define default options
const store = new Store({ defaultOpts: { env: process.env.NODE_ENV } })

// change default opts in runtime
store.opts.defaultOpts = { env: process.env.NODE_ENV }
```

3. prependDefaultConfiguration

Pass absolute filepath, which would be prepended. Useful to pass a directory with bundled default config

```js
store.prependDefaultConfiguration(filePath);
```

4. crash when configuration files can't be loaded

in 8+ behavior changes and malformed configuration files are not ignored anymore.
To disable this set crashOnError option to false`

```js
const { Store } = require('ms-conf');

const store = new Store({ crashOnError: false })
conf.prependDefaultConfiguration(['/path/to/config.json']);
conf.get('/path') // wont throw errors on malformed files, but will write into stderr notifying of the error
```

For a more detailed example - see tests

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