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.json file in the same folder as the main entry point (usually project root)
Just require getconfig like so from anywhere in your project:
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.
getconfig can fill those in for you. Just set the value of a key to reference the environment variable and it will be expanded inline. For example:
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.
When used in a Google Cloud Function or an AWS Lambda function, getconfig will use the
LAMBDA_TASK_ROOT environment variable, respectively, to automatically locate the appropriate root. This means that you can include your
config directory in your function deployment and things should work as you would expect them to.
Note: Google Cloud Functions automatically set
"production" when deployed, however Lambda leaves
NODE_ENV unset by default.
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.