Unitech / Unitech/pm2

PM2 NextJS cluster mode issue

Open
#5,240 7 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pm2.json configuration and reproduce the Docker command using PM2 v5.1.2, Node 14.x, and the Next.js entry point shown in the report. Trace how the cwd, script, and args are resolved; done means the production build is found under /usr/app/client and the application starts in cluster mode rather than failing from the pm2.json path.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, javascript, next.js, nodejs
Domain
backend, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.