1

I've been working on a Node.js project for several months and for some reason the logic I have set up stopped working. I'm not sure where to begin debugging the issue. My workaround has been to comment out the development lines when pushing to production.

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

if(env === 'development'){
    mongoose.connect('mongodb://localhost:27017/mean-demo');
  }else{
    mongoose.connect('mongodb://username:password@ds043270.mongolab.com:ds043270/location');
} 
byrdr
  • 5,197
  • 12
  • 48
  • 78
  • Are you sure you're firing up your node process with specifying the NODE_ENV the same as you used to?. i.e. are you calling `$ NODE_ENV=prod node index.js` or something along those lines? – Catfish Dec 22 '14 at 16:59

1 Answers1

1

first line should be var env = process.env.NODE_ENV || 'development';, although I guess that shouldn't really matter... but if you're going to use env anyway, not sure why you need to set process.env.NODE_ENV.

Also, make sure your environment is not accidentally already set using directions in this post: process.env.NODE_ENV is undefined to set it to development

But other than that you may find a module like dotenv helpful. Basically you make a .env file that has properties like Mongo URL details and it does NOT get committed to source control. You keep one .env in each environment.

Community
  • 1
  • 1
Jonathan Rowny
  • 7,588
  • 1
  • 18
  • 26
  • That did it thanks! After running it worked – byrdr Dec 22 '14 at 21:51
  • Good code correction. However, the method of not checking .env files into source control wouldn't work on Heroku, as far as I know, because the usual workflow for getting files into Heroku involves checking them in and pushing them up to Heroku. – stone Jun 22 '17 at 04:45