Iam trying to start a next.js app with 2 clusters in pm2. So mapped the mand 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._pile (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.
Iam trying to start a next.js app with 2 clusters in pm2. So mapped the mand 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._pile (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.
Share Improve this question edited Mar 9, 2020 at 15:32 CoryCoolguy 1,0799 silver badges18 bronze badges asked Mar 9, 2020 at 11:42 pauljebapauljeba 7702 gold badges11 silver badges27 bronze badges3 Answers
Reset to default 6It 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",
}]
}
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/
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.