Is there a programmatic way to know a node.js app is running in Heroku?

You can do this without setting custom environment variables. You can do it like this:

if (process.env._ && process.env._.indexOf("heroku") !== -1)
   console.log("I'm in Heroku!");

This is possible because on a Heroku dyno the _ environment variable is set to /app/.heroku/node/bin/node.


You use for that usual environment variables. Just set some variable on your heroku instance and check this:

process.env.HEROKU

On the heroku cli you would do: heroku config:set HEROKU=true

You can also set it on the web interface, see heroku docs for more: https://devcenter.heroku.com/articles/config-vars