cleverpaul
cleverpaul

Reputation: 935

How can I automatically restart a Node.js application using Forever and Nodemon (Windows)

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop.

I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal.

I installed both packages globally on my Windows 10 Device.

npm install forever -g
npm install -g nodemon

I tried using the following command to launch my app:

forever start nodemon --exitcrash app.js

However, I get the following error: "nodemon does not exist"

If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash.

Am I doing something wrong? Most advice I find online is only relevant to Linux systems.

Upvotes: 1

Views: 9772

Answers (2)

Rajat banerjee
Rajat banerjee

Reputation: 1821

Forever and nodemon achieve 2 completely different objectives

  • nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. more about that later

  • Forever, on the other hand, is for making your application run as a daemon in production. And auto restart if you have uncaught exceptions.

  • Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart

  • Current norm is to use PM2

Upvotes: 2

Anshul Verma
Anshul Verma

Reputation: 1091

If you are already using forever, then you can get rid of nodemon. Instead you can use a combination of forever and cluster module. Simply fork the worker in case of exceptions, and it makes your app more scalable too!

If still nodemon is preferable, maybe try installing it globally using the -g flag

Upvotes: 2

Related Questions