21

A web app I am writing in JavaScript using node.js. I use Foreman, but I don't want to manually restart the server every time I change my code. Can I tell Foreman to reload the entire web app before handling an HTTP request (i.e. restart the node process)?

pdjota
  • 3,163
  • 2
  • 23
  • 33

6 Answers6

23

Here's an adjusted version of Pendlepants solution. Foreman looks for an .env file to read environment variables. Rather than adding a wrapper, you can just have Foreman switch what command it uses to start things up:

In .env:

WEB=node app.js

In dev.env:

WEB=supervisor app.js

In your Procfile:

web: $WEB

By default, Foreman will read from .env (in Production), but in DEV just run this:

foreman start -e dev.env
  • 4
    This didn't work for me on Heroku-- the `.env` file was never loaded. Made a separate Procfile, instead: http://stackoverflow.com/questions/10560241/how-to-use-nodemon-with-env-files – Peter Ehrlich Sep 15 '12 at 03:59
7

You can use rerun for this purpose

You might implement just 2 commands for this:

  1. gem install rerun
  2. rerun foreman start

Then rerun will automatically restart process after any change in your files.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • Wouldn't this be slow, fully restarting everything in the procfile on every file change? – Peter Ehrlich Sep 15 '12 at 04:00
  • I currently use [supervisor](https://github.com/isaacs/node-supervisor) to this, it's not really a big deal to restart the app on every file update. It's very convenient. – Shane Stillwell Dec 05 '12 at 17:10
6

If you use nodemon , you can do

nodemon --exec "foreman start"
abel leroyer
  • 285
  • 2
  • 8
  • I tried this but it get stuck at "[nodemon] clean exit - waiting for changes before restart", the server dont start again – Bruno Lemos Mar 14 '17 at 01:19
3

The problem isn't with Foreman so much as it's with how node doesn't reload code on new requests. The solution is to use an npm package like supervisor along with an environment wrapper for Foreman.

First, install supervisor:

npm install -g supervisor

Then, write a wrapper shell script that Foreman can call:

if [ "$NODE_ENV" == "production" ]; then
  node /path/to/app.js
else
  supervisor /path/to/app.js
fi

Set the wrapper script's permissions to executable by running chmod a+x /path/to/wrapper_script.sh

Lastly, update foreman to use the wrapper script. So in your Procfile:

web: /path/to/wrapper_script.sh

Now when you run Foreman and your node app isn't running in production, it should reload on every request.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
Pendlepants
  • 199
  • 2
2

I feel like Peter Ehrlich's comment on the original question deserves to be an answer on its own. I think a different Procfile for local/dev is definitely the best solution: https://stackoverflow.com/a/10790514/133720

Community
  • 1
  • 1
Brade
  • 1,481
  • 1
  • 13
  • 21
0

You don't even need to install anything new if you use node-dev.

Your .env file loaded from Procfile:

NODECMD=node-dev

Your Procfile:

web: $NODECMD app/server.js

Your foreman command

foreman start -e dev.env -p 9786

And in your production env (heroku) set an environment variable:

NODECMD=node
ncabral
  • 2,512
  • 1
  • 19
  • 20