【问题标题】:How to run nuxt under pm2?pm2下如何运行nuxt?
【发布时间】:2019-01-23 07:06:28
【问题描述】:

我有 2 个需要在服务器上运行的 nuxt 项目。每当我在本地运行应用程序时,它似乎都在使用:npm run dev,但是在服务器上这需要在子进程下运行,所以我使用 pm2。但是每当我开始使用 pm2 运行相同的 npm 脚本时,进程就会出错。

用于此的命令是:sudo pm2 start npm --name "dev" -- dev,即使我单独运行应用程序也会出错。 sudo pm2 start npm --name "app1" -- app1:devsudo pm2 start npm --name "app2" -- app2:dev


package.json

{
    ...
    "scripts": {
        "app1:dev": "nuxt --config-file src/app1/nuxt.config.js -p=3000",
        "app2:dev": "nuxt --config-file src/app2/nuxt.config.js -p=4000",
        "dev": "concurrently \"npm run app1:dev\" \"npm run app2:dev\"",
    },
    "dependencies": {
        ...
    },
    "devDependencies": {
        "concurrently": "^3.6.0",
        "cross-env": "^5.2.0"
    }
}


pm2 日志

/home/ubuntu/.pm2/pm2.log :
PM2        | [2018-08-16T10:05:55.046Z] PM2 log: ===============================================================================
                                                                ...
PM2        | [2018-08-16T10:07:32.825Z] PM2 log: App [app1] with id [0] and pid [11135], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:32.827Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
PM2        | [2018-08-16T10:07:32.828Z] PM2 log: App name:app1 id:0 online
PM2        | [2018-08-16T10:07:33.105Z] PM2 log: App [app1] with id [0] and pid [11145], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:33.106Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
PM2        | [2018-08-16T10:07:33.108Z] PM2 log: App name:app1 id:0 online
PM2        | [2018-08-16T10:07:33.383Z] PM2 log: App [app1] with id [0] and pid [11155], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:33.383Z] PM2 log: Script /usr/local/bin/npm had too many unstable restarts (16). Stopped. "errored"

/home/ubuntu/.pm2/logs/app1-error.log :
/home/ubuntu/.pm2/logs/app1-out.log :
                            ...
0|app1   | Specify configs in the ini-formatted file:
0|app1   |     /home/ubuntu/.npmrc
0|app1   | or on the command line via: npm <command> --key value
0|app1   | Config info can be viewed via: npm help config
0|app1   |
0|app1   | npm@5.6.0 /usr/local/lib/node_modules/npm
0|app1   |
0|app1   | Usage: npm <command>
0|app1   |
0|app1   | where <command> is one of:
0|app1   |     access, adduser, bin, bugs, c, cache, completion, config,
0|app1   |     ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,
0|app1   |     explore, get, help, help-search, i, init, install,
0|app1   |     install-test, it, link, list, ln, login, logout, ls,
0|app1   |     outdated, owner, pack, ping, prefix, profile, prune,
0|app1   |     publish, rb, rebuild, repo, restart, root, run, run-script,
0|app1   |     s, se, search, set, shrinkwrap, star, stars, start, stop, t,
0|app1   |     team, test, token, tst, un, uninstall, unpublish, unstar,
0|app1   |     up, update, v, version, view, whoami
0|app1   |
0|app1   | npm <command> -h     quick help on <command>
0|app1   | npm -l           display full usage info
0|app1   | npm help <term>  search for help on <term>
0|app1   | npm help npm     involved overview
0|app1   |
                          ...

这一切意味着什么,pm2 不能识别 npm 命令吗?我在这里缺少一个参数吗? ...

额外信息:
服务器:Ubuntu 16.04
npm 版本:5.6.0
nuxt 版本:1.4.2
pm2 版本:3.0.3
节点版本:8.11.1

【问题讨论】:

    标签: node.js npm ubuntu-16.04 pm2 nuxt.js


    【解决方案1】:

    以下代码:

    pm2 start npm --name "anyName" -- run dev
    

    【讨论】:

    【解决方案2】:

    要让多个 nuxt 实例与 pm2 一起工作,您需要指向 node_modules/nuxt/bin/nuxt-start 中的 nuxt-start 脚本。

    This guide 解释得很好。 总结一下:您需要为您的应用程序 (ecosystem.config.js) 设置一个 生态系统 配置,您可以在其中为您的应用程序设置所有给定的参数。 Here 是一个包含所有可用参数的列表。

    你的应该是这样的:

    module.exports = {
      apps: [
        {
          name: 'app1',
          port: 3000,
          script: './node_modules/nuxt/bin/nuxt-start',
          cwd: '/home/user/your-nuxt-project/app1',
          env: {
            NODE_ENV: 'development'
          },
          env_production: {
            NODE_ENV: 'production'
          }
        },
        {
          name: 'app2',
          port: 4000,
          script: './node_modules/nuxt/bin/nuxt-start',
          cwd: '/home/user/your-nuxt-project/app2',
          env: {
            NODE_ENV: 'development'
          },
          env_production: {
            NODE_ENV: 'production'
          }
        }
      ]
    };
    

    然后只需 cd 到您的项目目录并运行 sudo pm2 start。它会自动找到配置文件并同时运行两个应用程序。

    【讨论】:

    • 这就像一个魅力,谢谢!对于有同样问题的人来说,该指南确实是必读的。
    • 赞成,您如何在此处指定模式,尽管我不断收到错误消息说没有 SSR 构建!请以nuxt start --spa 开头或使用nuxt build --universal 构建
    • "build": "nuxt build --universal" 添加到您的 package.json 中
    • 新版本的 nuxt 是否不再使用 nuxt-start ?我似乎在 node_modules/nuxt/bin 中找不到它。该文件夹中只有 nuxt.js。我正在使用 2.4.3
    • @AzDesign 不知道他们是否删除了它,在他们的发行说明中找不到它,但你可以在这里获得 nuxt-start 包npmjs.com/package/nuxt-start
    【解决方案3】:

    对于那些只想在生产模式下启动应用的用户:

    看到这个页面出现在如何使用 Nuxt.js 和 PM2 的搜索结果的顶部,我想我应该添加这个答案。

    尽管 PM2 是一个名为 ecosystem.config.js 的文件,但您需要添加到通用 Nuxt 应用程序以提供服务。在根项目目录中创建一个具有该名称的新文件并添加以下内容:

    module.exports = {
      apps: [
        {
          name: 'NuxtAppName',
          exec_mode: 'cluster', // Optional: If you want it run multiple instances.
          instances: 'max', // Or a number of instances.
          // 'max' auto detects how many CPU cores there are.
          // The previous option must exist to use the above.
          script: './node_modules/nuxt/bin/nuxt.js',
          args: 'start',
        },
      ],
    }
    

    现在使用npm run build 构建您的应用程序。

    并使用pm2 start 提供服务。

    检查状态pm2 ls

    您的 Nuxt.js 应用程序现在正在运行!

    Source

    【讨论】:

      【解决方案4】:

      虽然 JC97 的回答很好,但 Nuxt 已经升级,如果您想分别运行调试、开发、登台和生产设置,您需要一个更加划分的生态系统文件

      首先将 nuxt 安装为 npm install --save nuxt

      在项目文件夹中创建一个名为 config 的文件夹,该文件夹是服务器页面中间件和 nuxt 生成的其他目录的同级文件夹

      添加 4 个 .env 文件 .env.deb、.env.dev、.env.sta、.env.pro 用于调试、开发阶段和生产

      运行命令 pm2 Ecosystem 将生成默认文件

      用下面的替换默认文件

      它将每个环境创建为一个单独的应用程序

      如果您只想运行暂存版本,您可以将其运行为 pm2 start ecosystem.config.js --only myapp_sta

      如果你愿意,你可以同时运行所有 4 个!!!

      const dotenv = require('dotenv')
      
      const autorestart = true
      const watch = false
      const maxMemoryRestart = '512M'
      
      module.exports = {
        apps: [
          {
            name: 'myapp_dev',
            script: 'npm run clear && npm run dev',
            instances: 1,
            autorestart,
            watch,
            max_memory_restart: maxMemoryRestart,
            env: dotenv.config({ path: './config/.env.dev' }).parsed
          },
          {
            name: 'myapp_deb',
            script: 'npm run clear && npm run deb',
            instances: 1,
            autorestart,
            watch,
            max_memory_restart: maxMemoryRestart,
            env: dotenv.config({ path: './config/.env.deb' }).parsed
          },
          {
            name: 'myapp_sta',
            script: 'npm run clear && npm run sta',
            instances: 1,
            autorestart,
            watch,
            max_memory_restart: maxMemoryRestart,
            env: dotenv.config({ path: './config/.env.sta' }).parsed
          },
          {
            name: 'myapp_pro',
            script: 'npm run clear && npm run build && npm run start',
            instances: 1,
            autorestart,
            watch,
            max_memory_restart: maxMemoryRestart,
            env: dotenv.config({ path: './config/.env.pro' }).parsed
          }
        ],
      
        deploy: {
          myapp_dev: {
            user: 'zupstock',
            host: '192.168.1.103',
            ref: 'origin/master',
            repo: 'git@github.com:owner/myapp_v1.git',
            path: '/',
            'post-deploy':
              'cd myapp_v1 && npm install && pm2 startOrRestart ecosystem.config.js --only myapp_dev'
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

        如果可行,请尝试以下命令

        sudo pm2 start npm -- app1:dev
        

        【讨论】:

        • 这在 pm2 日志中给出了与我的问题中发布的相同的输出,status=errored 也是如此。 logs output
        【解决方案6】:

        对我来说,这适用于单个应用程序 https://nuxtjs.org/faq/deployment-pm2/

        module.exports = {
          apps: [
            {
              name: 'NuxtAppName',
              exec_mode: 'cluster',
              instances: 'max', // Or a number of instances
              script: './node_modules/nuxt/bin/nuxt.js',
              args: 'start'
            }
          ]
        }
        

        【讨论】:

          【解决方案7】:
          pm2 start ./node_modules/nuxt/bin/nuxt.js --name="<AppName>" -- start
          

          【讨论】:

            猜你喜欢
            • 2021-10-12
            • 2019-08-13
            • 2017-04-25
            • 2019-03-05
            • 2016-03-17
            • 1970-01-01
            • 2022-11-11
            • 1970-01-01
            • 2020-09-30
            相关资源
            最近更新 更多