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 module I’ve come across is the PM2 package. 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.

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.

{
    "apps" : [
            {
            "name": "koa-vue-notes-api",
            "script": "app.js",
            "instances": 2,
            "exec_mode": "cluster",
        }
    ]
}

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.

pm2 start ecosystem.json

Then to stop your app later just run

//use the app name you assigned in your config.
pm2 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.