/config

Load configurations from cli, environment & config files and validate them with a JSON schema.

Primary LanguageJavaScript

Config

Load configurations from cli, environment and config files and validate them with a JSON schema.

Per default environment variables are understood like this:

MYAPP_EMAIL__CLIENT_ID=123abc

gets parsed to

{
  email: {
    clientId: '123abc',
  },
}

Installation

npm install @datatypes/config

Usage

const Config = require('@datatypes/config')
const exampleConfig = new Config({
  appName: 'myapp'
})

exampleConfig
  .loadEnvironment({
    prefix: 'MYAPP_ALT_NAME_', // Default is MYAPP_
    pathSeparator: '--', // Default is __
    wordSeparator: '-', // Default is _
    casing: 'snake', // Default is camel
    encodeDataUris: false, // Default is true
  })
  .loadCliArguments()
  .loadDefaultFiles()
  .loadFile({
    filePath: '/path/to/config-file.yaml', // Supports .yaml, .json, .js
    isRequired: true, // Default is false
    ignoreIsDirectoryError: true, // Default is false
  })
  .merge({
    settingA: 'valueA',
  })
  // Replaces the values which contain a file path
  // of special keys with the file content of the file path
  .loadFilePathValues({
    triggerCharater: '%', // Default @
    shouldTrim: false, // Default true
  })

console.log(exampleConfig.config)

Default files

With increasing priority:

  • .myapp
  • .myapp{,.json,.yaml}
  • .myapp/config
  • .myapp/config{,.json,.yaml}
  • .config/myapp/config{,.json,.yaml} (This with .yaml is recommended)
  • .config/myapp/myapp{,.json,.yaml}
  • /.myapp{,.json,.yaml}
  • $(pwd)/.myapp{,.json,.yaml}

Related

  • nconf - Hierarchical node.js configuration with files, environment variables, command-line arguments and atomic object merging.
  • node-config - Node.js Application Configuration.
  • node-convict - Unruly configuration management for nodejs.
  • rc - The non-configurable configuration loader for lazy people.