Sunday, October 19, 2014

Keeping node applications running with PM2

Attention Node developers:  You can get rid of Forever and Nodemon now.

Discovering PM2 was one of those rare instances where I actually feel like technology can make my life easier.  It's a multi-purpose Node task manager that handles errors gracefully, restarts on changes, and provides a host of monitoring options.

Install:

npm install -g pm2

Develop:

pm2 start ./project/app.js --watch

Productionize:

pm2 start ./app1/app.js --name "application one"
pm2 start ./app2/app.js --name "application two"
pm2 start ./app3/app.js --name "application three"
pm2 startup

The final command will lead you through the short process of adding pm2 to your system's startup files, so that your applications will continue running on reboot.  No cron necessary.