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 =;configANALYTICS_ID = e12e;
Or maybe there's some code you want to run only while in development:
Install from NPM:
npm install environmentize
var e12e = ;
Get the current environment:
; // '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:
; // true; // false// You may use an array of environments as well; // true; // false
Get a value from an environment-value map:
; // 'foo'; // undefined// You can set a fallback value; // 'baz'
Get a value for a specific environment:
; // 'foo'; // undefined// With a fallback; // 'bar'// This works with multiple environments too; // 'foo'; // undefined; // 'bar'
Values may be functions, in which case they are called without arguments and the returned value is used:
; // 'foo'// Works for fallbacks too; // 'bar'// And for values in an environment-value map; // 'foo'
This way you can run a piece of code only in certain environments:
Environmentize also sets a method for each environment, which avoids the need for the first argument in some cases:
e12e; // 'foo'e12e; // 'bar'
This might be handy if you need to run some code only in a specific environment:
You can use your own environment definitions using
e12e;e12e; // 'foo'e12e; // 'bar'// Original environment methods are removede12e; // 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. : )