PM2 NextJS cluster mode issue
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
+1
Is there any resolution or workaround this issue?
+1 Same Here~t
+1
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 }
}
]
}
Try it:
"exec_mode": "cluster_mode"
when running
pm2-runtime start pm2.jsonin cluster mode, it wiil injectpm2.jsonto the script so the final command you run isnode_modules/next/dist/bin/next start -p 4000 pm2.jsonTry 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!