2

I'm currently trying to deploy my Webpack 2/React/Redux application on Heroku, and I'm trying to build locally then deploy to Heroku. The problem with this is that the PORT environment variable supplied by Heroku is set post build - so the process.env.PORT value is undefined when I build locally.

I could build on the Heroku server, but it's very slow and has a 60s boot timeout.

I was wondering if there was a way to set environment variables AFTER building?

David Tao
  • 61
  • 1
  • 7

1 Answers1

0

To build your app on Heroku, it is common to use the postinstall script in your package.json (See official docs for further details). Alternatively, you can add an npm script called heroku-postbuild.

In both cases, the $PORT environment variable should be set already and you do not have the 60s time out restriction during build.

Just to make sure I understand your question right: to read the environment variables during runtime is not possible after build (afaik), you would have to tell Webpack not to replace the process.env variables. But you can set them by a defined value during build time (e. g. configurable via the DefinePlugin, the EnvironmentPlugin or an alias).

UPDATE:

The$PORT environment variable is not set during the postinstall/heroku-postbuild execution time, therefore it is not recommended to hardly compile it into the code as mentioned before. But still, it is recommended to build your app on Heroku using one of postinstall/heroku-postbuild scripts.

If you want to run your app locally, you need to set your $PORT environment variable on your own, like this:

$> PORT=8000 node app.js
chrigu
  • 1
  • 2