Skip to content

datatypesjs/config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •