Forever + Nodemon running together
Asked Answered
C

8

53

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.

Cannikin answered 3/5, 2013 at 23:33 Comment(0)
O
54

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.

Ophthalmology answered 30/11, 2013 at 23:34 Comment(4)
I can't get this to work, Cannot start forever. script /current_working_directory/nodemon does not exist. Is there a way to fix this? I've installed nodemon both locally and globally via npm.Vibrate
I could not get this to work either, getting an error that the script nodemon didn't exist. So obviously it's trying to forever start nodemon, but I can't figure out how to make start app.js via nodemon. I guess I'll just live with the restart for each file change, but that seems rather inefficient.Bialy
in that case, it may be that the path of nodemon isn't being auto detected. Try: forever start /usr/bin/nodemon --exitcrash app.jsOphthalmology
I had to put quotes to mark nodemon --exitcrash as a single argument for forever forever start -c "nodemon --exitcrash" index.jsNashoma
B
64

You should run something like this

forever start -c nodemon app.coffee
Blinny answered 31/5, 2013 at 13:33 Comment(3)
You should see cfogelberg's answer on this question for a weakness in this solution. My answer has a workaround for it as well.Ophthalmology
not working for large number of users visit this #29537149Wangle
@Irtizashahid, it is not working if your app has large number of users. Your phrasing is just confusing as the above solution works in "normal" circumstances.Cannikin
O
54

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.

Ophthalmology answered 30/11, 2013 at 23:34 Comment(4)
I can't get this to work, Cannot start forever. script /current_working_directory/nodemon does not exist. Is there a way to fix this? I've installed nodemon both locally and globally via npm.Vibrate
I could not get this to work either, getting an error that the script nodemon didn't exist. So obviously it's trying to forever start nodemon, but I can't figure out how to make start app.js via nodemon. I guess I'll just live with the restart for each file change, but that seems rather inefficient.Bialy
in that case, it may be that the path of nodemon isn't being auto detected. Try: forever start /usr/bin/nodemon --exitcrash app.jsOphthalmology
I had to put quotes to mark nodemon --exitcrash as a single argument for forever forever start -c "nodemon --exitcrash" index.jsNashoma
P
15

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.

Preussen answered 26/5, 2015 at 12:0 Comment(0)
D
10

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.

Durmast answered 19/10, 2013 at 22:52 Comment(0)
O
5

I prefer a combo of what Toxa and Jubair suggest.

forever start -c nodemon app.coffee --exitcrash
Outbrave answered 14/1, 2015 at 19:38 Comment(2)
I have no idea why but this is the only command that has worked for me. I am new to this, and using express, and the default server is now at bin/www. So running forever start -c nodemon bin/www --exitcrash has finally got this working. Thank you!Natural
@akevit That is because that is how your server is set. That is customizable. You can set it up to run your server in the way that works best for you.Outbrave
P
2

If you need to pass arguments:

forever start -c "nodemon --harmony" app.js --exitcrash
Panther answered 28/4, 2015 at 14:39 Comment(0)
P
1

I'm using forever-service . . .

This is what worked for me. It does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):

$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"

Then I can run:

$ service raml start|stop|restart|status

I can also have the service start on server reboot with the chkconfig utility:

$ chkconfig --add raml
$ chkconfig raml on
Pilotage answered 15/4, 2015 at 20:39 Comment(1)
To watch for .js changes, you don't need the -e json,raml :). You don't need the -e option at all. nodemon will watch .js files by default.Pilotage
J
0

when using in the package.json use single quotes to make nodemon --existcrash as a single argument. "start": "forever -c 'nodemon --exitcrash' server.js"

Output: app_1 | [nodemon] app crashed app_1 | error: Forever detected script exited with code: 1 app_1 | error: Script restart attempt #1 app_1 | [nodemon] 1.19.4 app_1 | [nodemon] to restart at any time, enterrs app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] startingnode /app/server.js` app_1 | app is running on port 3000

`

Jacobo answered 8/12, 2019 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.