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