SKeney
SKeney

Reputation: 2301

Node/Express server deploy with Heroku and PM2

Attempting to add clustering ability via PM2 and deploy via my Node/Express application.

I've set up the following command:

pm2 start build/server/app.js -i max

The above works fine locally. I'm testing the functionality on a staging environment on Heroku via Performance 1X.

Heroku log

The above shows the log for the command but attempting 1 instance rather than max. It shows typical info after successfully running pm2 start however you can see app immediately crashes afterward.

Any advice or guidance is appreciated.

Upvotes: 1

Views: 2719

Answers (2)

Icekid
Icekid

Reputation: 505

I got the same error but I fixed it by adding

{
"preinstall":"npm I -g pm2",
"start":"pm2-runtime start build/server/app.js -i 1"

}

To my package.json file This is advised for production environment But running

pm2 start build/server/app.js -i max

Is for development purpose

Upvotes: 1

SKeney
SKeney

Reputation: 2301

I ended up using the following documentation: https://pm2.keymetrics.io/docs/integrations/heroku/

Using a ecosystem.config.js with the following:

module.exports = {
  apps : [
    {
      name: `app-name`,
      script: 'build/server/app.js',
      instances: "max",
      exec_mode: "cluster",
      env: {
        NODE_ENV: "localhost"
      },
      env_development: {
        NODE_ENV: process.env.NODE_ENV
      },
      env_staging: {
        NODE_ENV: process.env.NODE_ENV
      },
      env_production: {
        NODE_ENV: process.env.NODE_ENV
      }
    }
  ],
};

Then the following package.json script handles the deployment per the environment I am looking to deploy e.g. production:

"deploy:cluster:prod": "pm2-runtime start ecosystem.config.js --env production --deep-monitoring",

Upvotes: 1

Related Questions