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
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.
- Setting environments
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