How to start next.js app with pm2 in cluster mode using Yarn?
Asked Answered
V

3

6

Iam trying to start a next.js app with 2 clusters in pm2. So mapped the command yarn start to pm2 start yarn -i 2 --name "frontend" -- start

App starts and then crashes within a second.

[PM2] Starting /usr/bin/yarn in cluster_mode (2 instances)
[PM2] Done.
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name        │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ frontend    │ default     │ N/A     │ cluster │ 14901    │ 0s     │ 0    │ online    │ 0%       │ 33.8mb   │ user  │ disabled │
│ 1   │ frontend    │ default     │ N/A     │ cluster │ 14908    │ 0s     │ 0    │ online    │ 0%       │ 28.8mb   │ user  │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Output of .log file

0|frontend | /usr/share/yarn/bin/yarn:2
0|frontend | argv0=$(echo "$0" | sed -e 's,\\,/,g')
0|frontend |         ^^^^
0|frontend | 
0|frontend | SyntaxError: missing ) after argument list
0|frontend |     at Module._compile (internal/modules/cjs/loader.js:895:18)
0|frontend |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
0|frontend |     at Module.load (internal/modules/cjs/loader.js:815:32)
0|frontend |     at Function.Module._load (internal/modules/cjs/loader.js:727:14)
0|frontend |     at /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js:301:25
0|frontend |     at wrapper (/usr/local/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16)
0|frontend |     at next (/usr/local/lib/node_modules/pm2/node_modules/async/waterfall.js:96:20)
0|frontend |     at /usr/local/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
0|frontend |     at WriteStream.<anonymous> (/usr/local/lib/node_modules/pm2/lib/Utility.js:186:13)
0|frontend |     at WriteStream.emit (events.js:210:5)

Note: Starting the app as an npm application works, but in fork mode.

Vick answered 9/3, 2020 at 11:42 Comment(0)
C
6

It is not feasible to start pm2 cluster mode for npm/yarn script, you must use javascript file path as script for cluster. For next.js you can write the config like this:

module.exports = {
  apps: [{
    script: "node_modules/next/dist/bin/next",
    args: "start",
    exec_mode: "cluster",
  }]
}
Cloudless answered 5/4, 2021 at 15:37 Comment(0)
P
3
module.exports = {
    apps : [
        {
            name: "project",
            script: "./server.js",
            watch: true,
            interpreter: ".npm/yarn", //absolute path to yarn ; default is node
            interpreter_args: "",
            exec_mode: "cluster",
            cwd: "", //the directory from which your app will be launched
            args: "", //string containing all arguments passed via CLI to script
            env_development: {
                "PORT": 3000,
                "NODE_ENV": "development"
            },
            env_production: {
                "PORT": 8000,
                "NODE_ENV": "production",
            }
        }
    ]
}

any other parameters can been seen here:

https://pm2.keymetrics.io/docs/usage/application-declaration/

Present answered 10/3, 2020 at 10:31 Comment(0)
P
1

at project level create file ecosystem.config.js and set config below:

module.exports = {
    apps : [
        {
            name: "sa_page_local",
            mode: "cluster",
            script: "./server.js",
            watch: true,
            env_development: {
                "PORT": 3000,
                "NODE_ENV": "development"
            },
            env_production: {
                "PORT": 8000,
                "NODE_ENV": "production",
            }
        }
    ]
}
pm2 start ecosystem.config.js --env production

pm2 start ecosystem.config.js --env development

I didn't tried but just based on documentation, i wrote this scenario.

Present answered 9/3, 2020 at 14:58 Comment(1)
Thanks, but this doesn't say anything about running in yarn?Vick

© 2022 - 2024 — McMap. All rights reserved.