envlist
Micro-module (without dependency) for resolving the type of runtime environment between your application convention and another convention (like NODE_ENV
).
Why?
The problem
Like the debug of Express and some Express middlewares, few modules of Node.js require to put NODE_ENV
to production
or development
for changing the behavior of execution.
In the real world, it is common to use several environment names like local
, staging
, testing
.
Also it is common to use shortcuts and several environment names like prod
, dev
, stage
, test
, local
.
The problem is that in defining NODE_ENV
to local
(or another alias of a development
env), we do not get the full functionality of debugging of some modules.
Also in defining NODE_ENV
to prod
, we do not get the optimizations made to the production
environment.
The solution
A simple and useful solution is to map your application environment to a conventional NODE_ENV
environment.
Then consolidate all 💡
Install
npm install --save envlist
or
yarn add envlist
Usage
By default envlist
has preconfigured environments (of course you can replace it).
let EnvList = ;let envList = ;
List all types of environments
console;
Output:
local: APP_ENV: 'local' NODE_ENV: 'development' dev: APP_ENV: 'dev' NODE_ENV: 'development' stage: APP_ENV: 'stage' NODE_ENV: 'production' test: APP_ENV: 'test' NODE_ENV: 'development' prod: APP_ENV: 'prod' NODE_ENV: 'production' }
Customize a specific environment
You can customize each environment. Example by default the value of
envList.envs.test.NODE_ENV
is development
. If you want to change this value:
envListenvstestNODE_ENV = 'production';
envList.envs
it's just an object (see above).
Use your own set of environments
envListenvs = development: APP_ENV: 'development' NODE_ENV: 'development' production: APP_ENV: 'production' NODE_ENV: 'production' staging: APP_ENV: 'staging' NODE_ENV: 'production' test: APP_ENV: 'test' NODE_ENV: 'production' demoProd: APP_ENV: 'demoProd' NODE_ENV: 'production' demoDev: APP_ENV: 'demoDev' NODE_ENV: 'development' ;
Get a specific environment name
// devconsole;console; // developmentconsole;console; // prodconsole;console; // productionconsole;console;
Get the current environment
Considers that the process was launched with the command: node APP_ENV=dev app.js
// devenvListenv; // devenvListAPP_ENV; // developmentenvListNODE_ENV; /* { APP_ENV: 'dev', NODE_ENV: 'development' }*/envList;
Ensure the current environment
It may be useful to ensure that the current environment is good and change it if it's not the desired one.
Example, ensure the dev
environment:
envList;
In this example, if the current environment is not equal to dev
,
this method change and consolidate the current environment.
Does nothing if the current environment is equal to dev
.
Use your own resolver
Considers that you have installed Gulp and gulp-util
and you want to support env.type
:
let gutil = ; envList { envListenv = processenvAPP_ENV || processenvNODE_ENV || gutilenvtype; ifenvListenv && envList return this; throw 'Environment not found.';}
Use your own consolidator
Considers that you have installed Gulp and gulp-util
and you want consolidate also env.type
:
let gutil = ; envList { let current; if!envListenv envList; ifprocess && processenv current = envList; processenvAPP_ENV = currentAPP_ENV; processenvNODE_ENV = currentNODE_ENV; gutilenvtype = currentNODE_ENV; return envList;}
If you use a framework that handle the environment based on NODE_ENV
value (like Express.js),
consolidate the environment before loading Express.js.
Unit tests
envlist
is unit tested with Mocha and Unit.js.
Run the tests
cd node_modules/envlist && npm install && npm test
LICENSE
MIT (c) 2016, Nicolas Tallefourtane.
Author
Nicolas Talle |