Versatile, Predictable Configuration
config.get('best.configuration.library')
- JSON files
- Command Line Arguments
- Javascript Objects
- Environment Variables
- Programmatically
- Config properties are extended and replaced recursively
- Powerful debug trace of each config value's source
- Makes it easy to set defaults, load common configurations, and override with environment specific config.
Both get
and set
methods support dot paths, for example get('name.first')
Configuration can be selected at run-time for each of your environments, for example Production, Stage, Dev, Dev-Josh, Dev-Scott.
The run-time environment is determined by searching file paths and environment variables.
config.findRuntimeEnvironment({
paths:['./env-file'],
env: 'ENVIRONMENT',
default:'prod');
var config = require('pony-config');
config
.file('common-config.json') // common for all environments
.when('test').file('local-config.json'); // test only configuration
See more examples here.
npm install --save pony-config
The config module is a singleton. Set up your configuration in your main file at startup, access it everywhere in your project.
var config = require('pony-config');
See the api documentation for the full pony-config API documentation.
pony-conifg provides several tools to help you debug your configuration. See the debugging documentation for the debug API.
Always store key files outside of your repo
In production, keys are often served to applications through environment variables,
while locally keys live on the file system. To keep keys safe, store them outside your repo,
for example, in ~/keys/
and use pony-config to load the appropriate key.
config
.when('dev').file('~/keys/secret.key')
.when('prod').env( 'secret', 'SECRET_API_KEY')
pony-config uses
- minimist for command line argument parsing because it does exactly one thing and does it well.
- fs-coalesce to search file paths. This module (also by ponycode) extends the file path syntax to include '~', and automatically matches on the first extant file in an array of paths.
npm test
For coverage, run npm run coverage
Copyright (c) 2014,2020 PonyCode Corporation Licensed under the MIT license.