at.
at.

Reputation: 52510

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

Is there some variable or function I can call to know if a node.js application is running inside Heroku? Something like:

if (process.heroku)
  console.log("I'm in Heroku!");

Upvotes: 11

Views: 2422

Answers (3)

Wim Mostmans
Wim Mostmans

Reputation: 3601

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.

Upvotes: 12

Alvaro Cabrera
Alvaro Cabrera

Reputation: 21

On this specific case I've used if test -d /app; then npm run build; fi which can be used also inside npm-scripts.

Upvotes: 0

Ben A.
Ben A.

Reputation: 488

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

Upvotes: 13

Related Questions