Run Your Production Node Applications using PM2

Just entering the Node.js world? Even as a veteran, choosing modules to rely on in production can be a little intimidating.

One really polished and well-documented package I've come across is the PM2 module. You use PM2 to run your Node application in production.

When running your program with PM2 it will restart automatically when a crash or exception happens. Additionally, you can can view a really slick monitor display showing your all your apps and their current status and info. PM2 also makes running your Node.js applications in cluster mode incredibly easy.

Production studio

Let's go over some of the commands I use on my applications. Make sure you have PM2 and Node installed.

First you're going to want to create an ecosystem.json file in the root of your project. Use the following json as a template for you're program.

1{ 2 "apps": [ 3 { 4 "name": "koa-vue-notes-api", 5 "script": "app.js", 6 "instances": 2, 7 "exec_mode": "cluster" 8 } 9 ] 10}

Your ecosystem.json file is the configuration file that PM2 will use when dealing with your app.

To start your app the code is very simple:

1pm2 start ecosystem.json

Then to stop your app later just run

1# Use the app name you assigned in your config 2pm2 stop koa-vue-notes-api

Once running, you can view all your apps with pm2 list or enter the monitoring mode by using pm2 monit. To view information on your app run pm2 show followed by your app name from the config file.

Super simple and reliable - and the documentation is clearly written - which is a huge plus. Make sure to read through the details as this is just scratching the surface of what PM2 is capable of.

Home