问题
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!");
回答1:
You can do this without setting custom environment variables. You can do it like this:
if (process.env._ && process.env._.indexOf("heroku"))
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.
回答2:
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
回答3:
On this specific case I've used if test -d /app; then npm run build; fi which can be used also inside npm-scripts.
来源:https://stackoverflow.com/questions/28472113/is-there-a-programmatic-way-to-know-a-node-js-app-is-running-in-heroku