Use PM2 in Cloud Providers

Using PM2 in Cloud Providers

You might find yourself in a situation in which you do not have access to a raw CLI to start your Node.js applications. You have 2 ways to circumvent this:

  • Use the preinstall directive to install PM2 globally and start your application in the start script.
  • Require PM2 as a dependency and call the module via the start script.

Method 1: Pre install PM2 globally

The easiest way is to install pm2 globally via the preinstall scripts and start your application via pm2.

In package.json:

  "scripts": {
    "preinstall": "npm install pm2 -g",
    "start": "pm2 start app.js -i max --attach"
   },

For a fine tuned configuration look at Process Files.

Method 2: Require PM2 as a module

Depending on your cloud provider, the preinstall script might not be supported or pm2 might not be installed globally. To solve this we can require pm2 from the node_module folder:

Install pm2 in your projet using npm install --save pm2

In package.json:

  "scripts": {
    "start": "node ./node_modules/.bin/pm2 start app.js -i max --attach"
  }

You can set KEYMETRICS_PUBLIC and KEYMETRICS_SECRET in the environment variables so that once PM2 starts, it will automatically connect to Keymetrics. Or in bash mode:

export KEYMETRICS_PUBLIC="XXXX"
export KEYMETRICS_SECRET="YYYY"
pm2 update