Many people think shipping config json files is an upgrade over environment variables. It's not.
Dont't let your app load it's config.
![ruse](https://cloud.githubusercontent.com/assets/26752/2876431/c36febd8-d435-11e3-9159-26436bda3587.png)
..Inject it instead.
Unix environment vars are ideal for configration and I have yet to encounter an application that woudn't be better of with them.
- You can change a value at near-runtime:
DEBUG=*.* node run.js
- You can inject environment variables into a process belonging to a non-priviliged user:
source envs/production.sh && exec sudo -EHu www-data node run.js
- You can inherit, inside
staging.sh
, just source production.sh
, inside kevin.sh
source development.sh
- Your operating system is aware and provides tools for inspection, debugging, optionally passing onto other processes, etc.
And as with any other type of config:
- You can save them into files and keep them out of version control
One downside fo environment variables, is there is little convention and syntactical sugar in the high-level languages. This module attempts to change that.
Doesn't
Does
- Impose guidelines on dealing with environments
- Make vars available in nested format inside your app (e.g. MYAPP_REDIS_HOST) becomes config.redis.host
- Play well with unix
- Interpret multiple inherited bash environment files in an isolated environment for exporting to Nodejitsu or Heroku
Heroku example
Nodejitsu example
Conventions
Environmental tree:
_default.sh
├── development.sh
│ └── test.sh
└── production.sh
└── staging.sh.sh
On disk:
envs/
├── _default.sh
├── development.sh
├── production.sh
├── staging.sh
└── test.sh
This provides DRY flexibilty, but in practice I recommend using mainly
development.sh and production.sh, and duplication keys between them
so you can easily compare side by side.
Then just use _default.sh, test.sh, staging.sh for tweaks, to keep things
clear.
These variables are mandatory and have special meaning
- NODE_APP_PREFIX="MYAPP" # filter and nest vars starting with MYAPP right into your app
- NODE_ENV="production" # the environment your program thinks it's running
- DEPLOY_ENV="staging" # the machine you are actually running on
- DEBUG=. # Used to control debug levels per module
After getting that out of the way, feel free to start hacking on, prefixing all
vars with MYAPP a.k.a an actuall short abbreviation of your app name.
export NODE_APP_PREFIX="TLS"