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-runtime app.js -i max"

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-runtime app.js -i max"

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:

pm2 update

Set a fixed Server Name

You might want fixed server name so the names are constant in Keymetrics. The default link is the hostname ($HOST var) plus a few random characters. If you set the $PM2_MACHINE_NAME, the name will be fixed and used on the dashboard.

Be careful, in case of duplicate hostnames the dashboard will receive data from both instances and flicker.