How to automatically restart Node scripts when they crash in production?

Viewed 8403

The forever package appears stale (no commits in over 2 years) and its dependencies are out of date.

2 Answers

Your solution is PM2. It is a production process manager for Node applications, very easy to use. Installing PM2 with npm and running a simple command like pm2 start app.js you are almost done.

Additionally, you will have some useful tools, like logs and monitoring of your app status.

PM2 running application will be restarted automatically if the application crashes or is killed. Also, you can secure that your app will launch on server reboot, by adding it on system startup with pm2 startup systemd command.

A great package for this is pm2, but I encourage you to make your application more resilient to failures since it will certainly come at the cost of some downtime.

If you want to recover/ignore an error, a try/catch at the top of the code should do it.

Related