Environment aware config reader that follows our conventions at &yet.
Managing configs for different environments is kind of a pain.
In short I wanted it to:
NODE_ENVenvironment variable to grab appropriate config
var config = require('getconfig')from anywhere in the app and have it Just Work™
npm install getconfig
config/default.jsonfile in the same folder as the main entry point (usually project root)
var config = ;
Getconfig looks for a config directory in the same folder as the main entry point of your app. Your configuration files should be contained within that directory.
The configuration files attempted by require, in order, are:
Note that we don't list extensions, that's because the files are loaded via node's
require mechanism, so anything node can require will work.
In the event that
NODE_ENV is not set, getconfig will attempt to load
development in its place.
In a lot of situations it's simpler to pass configuration via environment variables, rather than hardcoding it into a config file.
Fortunately, getconfig can fill those in for you. Just set the value of a key to a string like
'$NODE_ENV' for example and the environment variable will be expanded inline.
Note that this will only work for environment variables whose names are within the character set of A-Z, 0-9, and _ (underscore). This is to prevent collisions with things like complex strings that may start with a
In certain circumstances, when your app isn't run directly (e.g. test runners) getconfig may not be able to lookup your config file properly. In this case, you can set a
GETCONFIG_ROOT environment variable to the directory where your config files are located.
getconfig will always fill in the
getconfig.env value in your resulting config object with the current environment name so you can programatically determine the environment if you'd like. If no
NODE_ENV is set it will also set
0.x.xrange per semver conventions.
devenviroments now look for related config files. So if you've set your
developmentand it will still find a file called
0.3.0- Switching from JSON.parse to ALCE to allow single quotes and comments. Better readme.