2

I am new to expressjs app development, now need to configure application as per environment. came across 'node-env-file' , 'cross-env'. but hardly understood anything. Pls suggest how to set env variables as per environment or some good documentation suggestions pls? Based on environment, I would like to load my configuraiton file.As of now, I have two config files, one for dev and one for production.

2 Answers 2

2

The idea is to set NODE_ENV as the environmental variable to determine whether the given environment is production or staging or development. The code base will run based on this set variable.

The variable needs to set in .bash_profile

$ echo export NODE_ENV=production >> ~/.bash_profile $ source ~/.bash_profile 

For more, check Running Express.js in Production Mode

I follow Ghost.org (a Node.js production) app's model.

Once that is done, you can have the environmental details in respective json files like config.production.json, config.development.json

Next, you need to load one of these file based on the environment.

var env = process.env.NODE_ENV || 'development'; var Nconf = require('nconf'), nconf = new Nconf.Provider(), nconf.file('ghost3', __dirname + '/env/config.' + env + '.json'); 

For more on how Ghost does this, check config/index.js

Sign up to request clarification or add additional context in comments.

1 Comment

oh yes, It helped me. Sorry for replying late.
1

I use a .env file with env vars:

VAR=VALUE 

And read that with source command before running express

$ source .env $ node app.js 

Then, to access inside express, you can use:

var temp = process.env.VAR; //contains VALUE 

You can use dotenv module, to automatically load .env file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.