Skip to content
Snippets Groups Projects
Sigmund, Dominik's avatar
Sigmund, Dominik authored
Fix constructor

Closes #2

See merge request !8
f798690c
History

config

Simple Config with ENV and Files Support.

Installation

  • npm install --save @libs/config

Usage

const Config = require('@libs/config')
let config = new Config([basePath], [EnvPrefix])

Then config is your config object. (Use it like config.setting)

It reads from the following sources, performing a deep merge:
(The Top Value overwrites the lower ones)

  • ENV
  • config.json
  • config.defaults.json

Enviroment Variables can target deep nested settings:
The Setting setting.deep.key can be reached with SETTING_DEEP_KEY

You may use the function _reload() to reload the config from all sources.
`config._reload()``

This makes _reload a reserved keyword

You may use the function _show() to display the config without:

  • password
  • secret
  • token
  • key
  • apiKey
  • apiToken
  • apiSecret
  • user
  • username

Values will be replaced with the value "redacted"

`config._show()``

This makes _show a reserved keyword

If you give a basePath, the config-Files are used from there.
Else the main dir of the application will be used.

YOu can give a prefix for the enviroment variables.
e.g. if you give "PREFIX" as prefix, the setting "setting.deep.key" can be reached with PREFIX_SETTING_DEEP_KEY

Values

You may use direct values like strings or numbers:

{
  "key": "value"
}

You can also use files to read the value from. This makes the config compatible with e.g. secrets:

{
  "key": "file:/path/to/file"
}

Examples

Only config.defaults.json

node examples/only-defaults/index.js

Only config.json

(Kind of legacy use)

node examples/only-local/index.js

defaults and config.json

node examples/defaults-overwrite/index.js

enviroment variables

(Enviroment set by command to not pollute your machine)

SETTING=overwritten-by-env node examples/env/index.js

enviroment variables with prefix

(Enviroment set by command to not pollute your machine)

PREFIX_SETTING=overwritten-by-env node examples/env_prefix/index.js

use files to read from

(Enviroment set by command to not pollute your machine)

SETTING=file:examples/files/setting_1.txt node examples/files/index.js