Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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!");
like image 268
at. Avatar asked Feb 12 '15 07:02

at.


People also ask

Does Heroku run npm start?

By default, Heroku runs npm start while starting deployed Node. js applications, but if you would like to run some other script from your package. json instead you just need to follow one simple step.

Does Heroku automatically npm install?

Heroku by default installs only the production dependencies, ignoring the development dependencies under devDependencies .

Is Heroku support NodeJS?

The Heroku Node. js buildpack is open source. For a better technical understanding of how the buildpack works, check out the source code at github.com/heroku/heroku-buildpack-nodejs.


2 Answers

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.

like image 132
Wim Mostmans Avatar answered Oct 19 '22 11:10

Wim Mostmans


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

like image 40
Ben A. Avatar answered Oct 19 '22 10:10

Ben A.