pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

PM2 NextJS cluster mode issue

Open smasilamani-cfins opened this issue 4 years ago • 7 comments

I am trying to run NextJS in Docker with pm2 and get the below error. anybody have any luck running this in cluster mode?

My Docker command looks like this : CMD [ "pm2-runtime" ,"start","pm2.json"]

Also I noticed that when I use "pm2-runtime start "./node_modules/next/dist/bin/next start" -i 4" it starts the application in fork mode instead of cluster mode . As per the documentation, passing option i should start the process in cluster mode which is not what I see.

PM2 : v5.1.2 Node : v14.x

pm2.json file

{
  "apps": [
    {
      "name": "App-Client",
      "cwd": "/usr/app/client",
      "script": "node_modules/next/dist/bin/next",
      "args": "start -p 4000",
      "instances": "max",
      "exec_mode": "cluster",
      "kill_timeout": 5000,
      "env": { "NODE_ENV": "production", "production": true }
    }
  ]
}

Error: (As you can see its looking for .next folder inside pm2.json folder)

2021-12-03T04:31:51: PM2 log: App [App-Client:0] exited with code [1] via signal [SIGINT] 2021-12-03T04:31:51: PM2 log: App [App-Client:0] starting in -cluster mode- Error: Could not find a valid build in the '/usr/app/client/pm2.json/.next' directory! Try building your app with 'next build' before starting the server. at Server.readBuildId (/usr/app/dip/client/node_modules/next/next-server/server/next-server.ts:1671:15) at new Server (/usr/app/dip/client/node_modules/next/next-server/server/next-server.ts:179:25) at createServer (/usr/app/dip/client/node_modules/next/server/next.ts:41:10) at start (/usr/app/dip/client/node_modules/next/server/lib/start-server.ts:9:15) at exec (/usr/app/dip/client/node_modules/next/cli/next-start.ts:53:3) at /usr/app/dip/client/node_modules/next/bin/next.ts:107:36 2021-12-03T04:31:51: PM2 log: App name:App-Client id:1 disconnected

smasilamani-cfins avatar Dec 04 '21 16:12 smasilamani-cfins

+1

KristjanVall avatar Feb 10 '22 12:02 KristjanVall

Is there any resolution or workaround this issue?

AnubhavGuptaVeative avatar Apr 21 '22 17:04 AnubhavGuptaVeative

+1 Same Here~t

cizz3007 avatar Oct 05 '22 06:10 cizz3007

+1

miguelvictor avatar Nov 04 '22 14:11 miguelvictor

when running pm2-runtime start pm2.json in cluster mode, it wiil inject pm2.json to the script so the final command you run is node_modules/next/dist/bin/next start -p 4000 pm2.json

Try to modify the args attribute to start -p 4000 ./ as workaround

{
  "apps": [
    {
      "name": "App-Client",
      "cwd": "/usr/app/client",
      "script": "node_modules/next/dist/bin/next",
      "args": "start -p 4000 ./",
      "instances": "max",
      "exec_mode": "cluster",
      "kill_timeout": 5000,
      "env": { "NODE_ENV": "production", "production": true }
    }
  ]
}

ycscholes avatar Nov 18 '22 06:11 ycscholes

Try it: "exec_mode": "cluster_mode"

maarekj avatar Dec 21 '23 13:12 maarekj

when running pm2-runtime start pm2.json in cluster mode, it wiil inject pm2.json to the script so the final command you run is node_modules/next/dist/bin/next start -p 4000 pm2.json

Try to modify the args attribute to start -p 4000 ./ as workaround

{
  "apps": [
    {
      "name": "App-Client",
      "cwd": "/usr/app/client",
      "script": "node_modules/next/dist/bin/next",
      "args": "start -p 4000 ./",
      "instances": "max",
      "exec_mode": "cluster",
      "kill_timeout": 5000,
      "env": { "NODE_ENV": "production", "production": true }
    }
  ]
}

the thing with ./ at the end really works!!! finally! thank you!

jjjuk avatar Jan 12 '24 17:01 jjjuk