Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku NODE_ENV environment variable

Tags:

node.js

heroku

I'm running a Node project with Heroku as my main deploy target. In dev environment I'm using grunt to start the web server, however in production I prefer to launch the app directly with node app

Here's my Procfile:

web: bin/web

And bin/web:

#!/bin/sh                                                                       
echo "NODE_ENV=" $NODE_ENV
if [ "$NODE_ENV" == "production" ]; then
    echo "Starting the server with node app"
    node app
else
    echo "Starting the server using grunt"
    grunt
fi

The first echo is for debugging. heroku log is showing:

app[web.1]: NODE_ENV=

Basically, meaning NODE_ENV is not set. (and the app starts with grunt instead of node app)

The docs say that "The NODE_ENV environment variable defaults to production, but you can override it if you wish"

What am I missing?

like image 906
Konstantin K Avatar asked Feb 17 '14 14:02

Konstantin K


People also ask

What is NODE_ENV variable?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).

How do I set ENV on Heroku?

Heroku Web Interface The first step is to log into your account and go to the Heroku dashboard. Figure 1 illustrates my dashboard. Choose the application for which you want to set the environment variables. Once you select the application, it takes you to the overview page of that project.

What does NODE_ENV default to?

We see that it in fact reads NODE_ENV and defaults to 'development' if it isn't set. This variable is exposed to applications via 'app.

Should I commit Node_modules to Heroku?

I am the Node language owner at Heroku and the answer is simple: No. Do not check node_modules in to Heroku apps.


1 Answers

Not sure if you figured this out or not, but I had the same problem and fixed it using:

heroku config:set NODE_ENV=production

Via https://devcenter.heroku.com/articles/nodejs-support.

Good luck!

like image 125
Chris Harrington Avatar answered Sep 22 '22 21:09

Chris Harrington