Hi Guy’s Welcome to Proto Coders Point. In this nodejs article let’s look into how to make nodemon automatically get restart when the program crash without waiting for file changes.
Basically many Nodejs Developer makes use of Nodemon while developing & testing nodejs application so that they can make use of feature i.e. Nodemon automatically restart the application when it finds any code file changes.
However, If the nodejs program crashes due to some error, Nodemon will stop and give use this error on console screen:
nodemon app crashed - waiting for file changes before starting
The above info about app crash update on screen it very useful for developer to understand at which line of code nodejs is getting crashed.
support if the nodemon auto restart immediately after showing above error message “nodemon app crashed” on screen, the developer will never be able to debug the issue and code will run with endless error loop.
How to make nodemon automatically get restart when the program crash without waiting for file changes.
Suppose you are aware of this & want’s nodemon to automatically restart the nodejs application on crash, then you can simple run node application by using below command:
Windows
nodemon -x 'node index.js || copy /b index.js +,,'
Linux / Mac
nodemon -x 'node index.js || touch index.js'
pm2 restart node script on crash
Many nodejs developer make use of pm2 (process manager for node.js) in production, To keep their node application script running in background.
if you use pm2 to run you nodejs application and want’s it to auto restart on crash then use below command:
pm2 start index.js --watch