Environment config file on node.js

Posted in Node.js, on 5/4/2014

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

Everything is on following link.

https://www.npmjs.org/package/node-env-file

 

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

PORT=9000
NODE_ENV=development

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');

Tips

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


IP=''

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


process.env.IP || '127.0.0.1'