# node-config-loader

> Scan directories and loads json and yaml files

Latest version **3.1.0** (published 2017-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-config-loader
pnpm add node-config-loader
yarn add node-config-loader
bun add node-config-loader
```

## 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 | 3.1.0 |
| Published | 2017-04-09 |
| First published | 2014-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Stefan Zerkalica |
| Maintainers | zerkalica |
| Keywords | yaml, json, config, loader |

## Links

- npm: https://www.npmjs.com/package/node-config-loader
- Repository: https://github.com/zerkalica/node-config-loader
- Issues: https://github.com/zerkalica/node-config-loader/issues
- npm.io page: https://npm.io/package/node-config-loader

## Dependencies (6)

- [toml](https://npm.io/package/toml.md) ^2.3.2
- [debug](https://npm.io/package/debug.md) ^2.6.3
- [globby](https://npm.io/package/globby.md) ^6.1.0
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.8.3
- [find-root](https://npm.io/package/find-root.md) ^1.0.0
- [loader-utils](https://npm.io/package/loader-utils.md) ^1.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.1.0 (latest) — 2017-04-09
- 3.0.8 — 2017-02-10
- 3.0.7 — 2016-12-21
- 3.0.6 — 2016-10-03
- 3.0.5 — 2016-10-01
- 3.0.4 — 2016-10-01
- 3.0.3 — 2016-09-29
- 3.0.2 — 2016-09-29
- 3.0.1 — 2016-09-28
- 3.0.0 — 2016-09-28
- 2.1.0 — 2016-05-13
- 2.0.7 — 2016-04-07
- 2.0.6 — 2016-02-13
- 2.0.5 — 2016-02-13
- 2.0.3 — 2016-01-13
- … 18 more at https://npm.io/package/node-config-loader/versions

## README

Node config loader [![Build Status](https://secure.travis-ci.org/zerkalica/node-config-loader.png)](http://travis-ci.org/zerkalica/node-config-loader)
======================================================================================================================================================

[![NPM](https://nodei.co/npm/node-config-loader.png?downloads=true&stars=true)](https://nodei.co/npm/node-config-loader/)

Scan directories, load, parse to js object and merge many configs into single file/object.

-	Highly customizable and composable: each component is a pure function and exposed to public: compose you own loaders
-	Used [globby](https://github.com/sindresorhus/globby) for files matching
-	Compatible with [lorenwest node-config](https://github.com/lorenwest/node-config/wiki/Configuration-Files) file loading scheme, but each file name can be prefixed by '#' separator
-	Default loader supports json and yml files via [nodeca js-yaml](https://github.com/nodeca/js-yaml) (can be overrided)
-	Live reload support via webpack loader

## Config merge example

```yaml
# input1#dev.yaml
ns:
    to:
        name: test
testArr:
    - t1
    - t2
__push__: [testArray2]
testArray2:
    - test1
    - test2
```

```yaml
# input2#dev.yaml

ns:
    to:
        email: test-email
testArr:
    - t3
testArray2:
    - test3
```

merged input1 + input2:

```yaml
#output.yaml

ns:
    to:
        name: test
        email: test-email
testArr:
    - t3
testArray2:
    - test1
    - test2
    - test3
```

Usage
=====

As common lib
-------------

```js
//simple.js
import {loadConfig} from 'node-config-loader'
import os from 'os'

loadConfig({
    mask: [
        `${__dirname}/config/**/*.{json,yml,tml}`
        `/etc/myapp.d/**/*.{json,yml,tml}`
        `${process.env.HOME}/.config/myapp/**/*.{json,yml,tml}`
    ],
    instance = 'server',
    env = process.env.NODE_ENV,
    hostname = os.hostname(),
    tagSeparator = '#'
})
    .then(config => console.log(config))
    .catch(err => config.error(err.message))
```

As webpack loader
-----------------

Config load order:

1.	.configloaderrc
2.	webpack.config.js configLoader section
3.	webpack loader query params

```js
import config from 'node-config-loader/webpack?env=prod&instance=client!./.configloaderrc'
console.log(config)
```

Where .configloaderrc is

```json
{
    "mask": [
        "{ROOT}/src/config/**/*.json"
    ],
    "instance": "client|server",
    "env": "prod|dev",
    "hostname" : "host",
    "tagSeparator": "#"
}
```

*mask* is required, all other params are optional.

Available env vars in mask:

-	{ROOT} - project root
-	{DIRNAME} - .configloaderrc directory
-	{PWD} - process.cwd()
-	any process.env variable

Via Webpack config
------------------

```js
// webpack.config.js
module.exports = {
    plugins: [
        new LoaderOptionsPlugin({
            options: {
                configLoader: {
                    env: isProduction ? 'prod' : 'dev',
                    instance: process.env.APP_INSTANCE || 'client'
                }
            }
        }),
    ],
    module: {
        loaders: [
            {
                test: /.*\.configloaderrc$/,
                loader: 'node-config-loader/webpack'
            }
    }
}
```

## Flowtype

npm install --save empty

.flowconfig

```ini
[options]
module.name_mapper='.*\(\.configloaderrc\)' -> 'empty/object'
```

Isomorphic friendly сlient with run-time config
-----------------------------------------------

```js
// getConfig.js
import config from '../conf/.configloaderrc'
import {merge} from 'node-config-loader'

function getRuntimeConfig({settings, location, referrer}) {
    return {
        env: {
            origin: location.origin,
            hash: location.hash,
            pathname: location.pathname,
            search: location.search,
            referrer: referrer
        },
        config: {
            debug: settings.debug,
            sitePrefix: settings.sitePrefix,
            locale: {
                lang: settings.locale
            }
        }
    }
}

export default function getConfig(opts) {
    return merge(config, getRuntimeConfig(opts))
}
```

```js
// index.js
import getConfig from './getConfig'

const config = getConfig({
    settings: window.settings || {},
    location: window.location,
    referrer: document.referrer
})

// config
init(config)
```

## interfaces

```js
// @flow

function merge(objects: Object[]): Object
function strMap(strs: string, templateArgs: {[id: string]: string}): string

interface CreateScannerOpts {
    merge?: (acc: Object, src: Object) => Object;
    parser?: (data: FileRec) => Promise<Object>;
    readFile?: (fileName: string) => Promise<Buffer>;
}

type Scanner = (files: string[]) => Promise<Object>

function createScanner(opts?: CreateScannerOpts): Scanner

interface CreateNodeFilterOpts {
    instance?: string;
    hostname?: string;
    tagSeparator?: string;
    env?: string;
    templates?: string[];
}
function createNodeFilter(opts: CreateNodeFilterOpts): (files: string[]) => string[]

interface GetFilesOptions extends CreateNodeFilterOpts {
    mask: string[];
    glob?: {
        cwd?: string;
        root?: string;
        dot?: string;
        nomount?: boolean;
        mark?: boolean;
        nosort?: boolean;
        stat?: boolean;
        readdir?: boolean;
        silent?: boolean;
        statCache?: Object;
        symlinks?: Object;
        debug?: boolean;
        nonull?: boolean;
        nounique?: boolean;
        nobrace?: boolean;
        noglobstar?: boolean;
        noext?: boolean;
        nocase?: boolean;
        matchBase?: boolean;
        nodir?: boolean;
        ignore?: string;
        follow?: boolean;
        realpath?: boolean;
        absolute?: boolean;
    }
}
function getFiles(opts: GetFilesOptions): Promise<string[]>;

interface LoadConfigOptions extends GetFilesOptions, CreateScannerOpts {
    mask: string[];

    instance?: string;
    hostname?: string;
    tagSeparator?: string;
    env?: string;
    templates?: string[];

    glob?: Object;

    merge?: (acc: Object, src: Object) => Object;
    parser?: (data: FileRec) => Promise<Object>;
    readFile?: (fileName: string) => Promise<Buffer>;
}
function loadConfig(opts: LoadConfigOptions): Promise<Object>
```

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