Use PM2 in Cloud Providers

Using PM2 on Cloud Providers

You might find yourself in a situation in which you do not have access to the CLI to start your Node.js applications.

In such a situation, pm2 must be added as a dependency and must be called with the start script.

Prepare your app

Set your ecosystem file

Generate an ecosystem.config.js template with:

pm2 init

Modify the ecosystem file to match your needs:

module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }]
}

Learn more about ecosystem file here.

Add PM2 as a module

Add pm2 as a dependency to your project.

With npm:

npm install pm2

With yarn:

yarn add pm2

Start script in package.json

In your package.json, modify your start script like the following:

{
  "scripts": {
    "start": "pm2-runtime start ecosystem.config.js --env production"
  }
}

Deploy your app

You can now deploy your application in your cloud providers like you would have done for a regular node.js app.

Contribute to this page