Search code examples
node.jsherokupm2

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.


Solution

  • 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",