【问题标题】:How to start next.js app with pm2 in cluster mode using Yarn?如何使用 Yarn 在集群模式下使用 pm2 启动 next.js 应用程序?
【发布时间】:2020-03-09 11:42:12
【问题描述】:

我正在尝试在 pm2 中启动具有 2 个集群的 next.js 应用程序。所以将命令yarn start映射到pm2 start yarn -i 2 --name "frontend" -- start

应用程序启动,然后在一秒钟内崩溃。

[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 │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

.log 文件的输出

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)

注意:将应用程序作为 npm 应用程序启动是可行的,但在 fork 模式下。

【问题讨论】:

    标签: javascript node.js next.js yarnpkg pm2


    【解决方案1】:

    npm/yarn脚本启动pm2集群模式是不可行的,必须使用javascript文件路径作为集群脚本。对于 next.js,您可以像这样编写配置:

    module.exports = {
      apps: [{
        script: "node_modules/next/dist/bin/next",
        args: "start",
        exec_mode: "cluster",
      }]
    }
    

    【讨论】:

      【解决方案2】:
      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",
                  }
              }
          ]
      }
      

      任何其他参数都可以在这里看到:

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

      【讨论】:

        【解决方案3】:

        在项目级别创建文件ecosystem.config.js 并在下面设置配置:

        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
        
        

        我没有尝试,只是根据文档,我写了这个场景。

        【讨论】:

        • 谢谢,但这并没有说明在纱线中运行?
        猜你喜欢
        • 1970-01-01
        • 2022-08-06
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        • 2020-04-26
        • 1970-01-01
        • 2018-02-25
        • 2017-10-01
        相关资源
        最近更新 更多