How to Use Different Environment Config with Node.js

2014/5/31 min read
bookmark this
Responsive image

Depend on running enviroment, use different configuration files. At node.js world, following module is one of the choice.

Everything is on following link.


1. Basically, what need to be have is, you need to install node-env-file module. 

npm install node-env-file --save

This command will update package.json and install
 node-env-file module. 

2. Create your enviroment file, like .env. The name could be any name. .env_dev, env_prod


3. add following code at node.js server side code. This  will use the  node-env-file module to replace your  process.env.{your variable}.

var env = require('node-env-file');
env(__dirname + '/.env');


When you want to setup the value to empty, you can set the value like this, in this way.


If you code is like following, the will use instead.

process.env.IP || ''