Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set proccess.env in Node express app on heroku

I have a simple node with mongo (via mongojs) app that is developed locally and deployed on heroku. In my development environment, i want to use a local instance of mongo, while in production, I would like to use the instance heroku provides to me via "process.env.MONGOLAB_URI".

My current approach is that I would set the datavase url depending on the environment variable, but how do i actually go into production mode? Moreover, how can i configure this so that when i develop on my local machine its development mode, when i upload to heroku its production mode?

app.configure('production', function(){
  // ...
  databaseUrl = "mydb"; // the default
});

app.configure('development', function(){
  // ...
  databaseUrl = process.env.MONGOLAB_URI;
});

db = require("mongojs").connect(databaseUrl);
like image 363
kumikoda Avatar asked Mar 20 '13 01:03

kumikoda


People also ask

Does .ENV work on Heroku?

As such, creating an . env file on Heroku isn't a good approach. Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable.

How do I activate environment in Heroku?

Go to your project: Open a terminal window and navigate to your project folder: cd my_project. Create the virtual environment: virtualenv venv. Activate the virtual environment: source venv/bin/activate.


1 Answers

Set the NODE_ENV environment variable to "development" on your local environment, and set it to "production" on Heroku. https://devcenter.heroku.com/articles/nodejs#setting-node-env

like image 113
Joe Friedl Avatar answered Sep 21 '22 07:09

Joe Friedl