Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
I needed to run a Node project and, if it failed for some reason, run it again.
I thought of using nodemon, which restarts a Node process when a file changes.
I assumed it would do the same when the process crashed, but it’s not how it works.
So I found this solution.
If the process crashes, I use the command line to run the touch command on the main app file, so nodemon detects a change in the file and restarts the process:
nodemon -x 'node app.js || touch app.js'
Simple, and it works.
Of course in a real environment you’d use a robust solution like pm2 (see my tutorial how to use pm2 to serve a Node.js app) but this is something I need to run for a couple hours on my local machine and it works.
Update: an alternative is using Forever https://www.npmjs.com/package/forever