Utilities for handling differences between environments.
Utilities for handling value and behavior differences between environments.
environmentize helps you to set different values for each environment
your code runs in. For example, you probably have different database
settings for each environment or you probably want to use an analytics
code only in production.
environmentize helps you getting it done:
configDB_HOST = e12eproduction : '123.456.789.3'staging : '123.456.789.4''127.0.0.1';configANALYTICS_ID = e12eproduction'X-12345-6' null;
Or maybe there's some code you want to run only while in development:
e12edevelopment// This will run only if you are in development.console.log'This is the development environment.';;
Install from NPM:
npm install environmentize
var e12e = require'environmentize';
Get the current environment:
e12e; // 'development'// ore12eenv; // 'development'
environmentize will use the
NODE_ENV environment variable
to get the environment in which the code is running. You shouldn't ever
need to change
By default, environmentize sets five environments:
'production'. If you have a different
environment structure, you can change them using
NOTE: The following examples assume we are in the
environment, unless stated otherwise.
Check if the code is being run in a given environment:
e12e'development'; // truee12e'integration'; // false// You may use an array of environments as welle12e'test' 'development'; // truee12e'staging' 'production'; // false
Get a value from an environment-value map:
e12edevelopment : 'foo'production : 'bar'; // 'foo'e12eintegration : 'foo'production : 'bar'; //// You can set a fallback valuee12eintegration : 'foo'production : 'bar''baz'; // 'baz'
Get a value for a specific environment:
e12e'development' 'foo'; // 'foo'e12e'integration' 'foo'; //// With a fallbacke12e'integration' 'foo' 'bar'; // 'bar'// This works with multiple environments tooe12e'test' 'development' 'foo'; // 'foo'e12e'staging' 'production' 'foo'; //e12e'staging' 'production' 'foo' 'bar'; // 'bar'
Values may be functions, in which case they are called without arguments and the returned value is used:
e12e'development' return 'foo'; ; // 'foo'// Works for fallbacks tooe12e'integration' return 'foo'; return 'bar'; ; // 'bar'// And for values in an environment-value mape12ereturn 'foo';; // 'foo'
This way you can run a piece of code only in certain environments:
e12e'development'// Do something that should only be done during development.console.log'This is the development environment';;
Environmentize also sets a method for each environment, which avoids the need for the first argument in some cases:
e12edevelopment'foo'; // 'foo'e12eintegration'foo' 'bar'; // 'bar'
This might be handy if you need to run some code only in a specific environment:
e12eproduction// This code will run only in production.appusetoobusy;;
You can use your own environment definitions using
e12esetupenvironments : 'dev' 'ci' 'stg' 'prod'defaultEnv : 'dev';e12edev'foo'; // 'foo'e12eci'foo' 'bar'; // 'bar'// Original environment methods are removede12edevelopment'foo'; // throws error
Environmentize's tests are written in mocha and can be run with:
Feel free to submit any patches or report any issues. I'll do my best to check them as quick as possible (in a few days, usually). When submitting a patch, please add your name and link to the author section below.
I would greatly appreciate if someone could double check the tests. I'm sure there's plenty of room for improvements there.
Issues and patches regarding grammar errors in code comments and docs are welcome as well. : )