Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forever + Nodemon running together

Is there any way to have both of this packages running together?

So basically I want to have best from both worlds. Running server automatically (and restarting when there is an error) and also automatic updates when there is .js file change happening.

like image 625
Patrik Fuhrmann Avatar asked May 03 '13 23:05

Patrik Fuhrmann


People also ask

How do I stop a Nodemon server in terminal?

Press Ctrl + C to exit from Nodemon on windows.

Why do you use forever with node js?

The purpose of Forever is to keep a child process (such as your node. js web server) running continuously and automatically restart it when it exits unexpectedly.


4 Answers

You should run something like this

forever start -c nodemon app.coffee
like image 123
Anton Borodatov Avatar answered Oct 12 '22 04:10

Anton Borodatov


Toxa was on the right track, the issue that cfogelberg raised is valid, but to avoid that issue you can do the following:

forever -c "nodemon --exitcrash" app.js

this makes sure nodemon actually exits (rather than giving you the "app crashed" message) and then forever picks it up again.

In forever --help this -c specifies a command to run otherwise it defaults node. Without -c results in the error that is mention in the comments to this answer.

like image 30
Jubair Avatar answered Oct 12 '22 02:10

Jubair


There is an entry about it in the nodemon FAQ:

If you're using nodemon with forever (perhaps in a production environment), you can combine the two together. This way if the script crashes, forever restarts the script, and if there are file changes, nodemon restarts your script. For more detail, see issue 30.

To achieve this you need to add the following on the call to forever:

  • Use forever's -c nodemon option to tell forever to run nodemon instead of node.
  • Include the nodemon --exitcrash flag to ensure nodemon exits if the script crashes (or exits unexpectedly).
  • Tell forever to use SIGTERM instead of SIGKILL when requesting nodemon to stop. This ensures that nodemon can stop the watched node process cleanly.
  • Optionally add the --uid parameter, adding a unique name for your process. In the example, the uid is set to foo.

bash forever start --uid foo --killSignal=SIGTERM -c nodemon --exitcrash server.js

To test this, you can kill the server.js process and forever will restart it. If you touch server.js nodemon will restart it.

To stop the process monitored by forever and nodemon, simply call the following, using the uid we assigned above (foo):

bash forever stop foo

This will stop both nodemon and the node process it was monitoring.

Note that I would not recommend using nodemon in a production environment - but that's because I wouldn't want it restart without my explicit instruction.

like image 15
mik01aj Avatar answered Oct 12 '22 02:10

mik01aj


I have not found a way of getting both packages running together. I tried to do @toxa's technique, but when my node.js app threw an exception nodemon would not automatically restart it, instead outputting an error message to the forever log:

nodemon] app crashed - waiting for file changes before starting...

However, forever has a -w option and the following command is effectively the same as if I'm running nodemon and forever together:

forever start -w my-app.js

The downside of forever -w versus nodemon: forever does not have a --delay option, so my server gets restarted once for each file that is changed.

like image 10
cfogelberg Avatar answered Oct 12 '22 03:10

cfogelberg