【问题标题】:How can I fix issues launching an app in PM2 on Windows?如何解决在 Windows 上的 PM2 中启动应用程序的问题?
【发布时间】:2020-04-25 07:00:17
【问题描述】:

我正在尝试通过 IIS10 上的 PM2 在 Windows Server 2016 上使用 Node.js 版本 12.14.0 运行 Docusaurus。我正在使用 PM2,因此我可以在服务器重新启动后重新启动应用程序。我现在不得不承认,今天是我第一次尝试使用 Node.js,所以请多多包涵。

这是 usually run,在 Docusaurus 目录中调用 npm run start。在测试 Node.js 以及“Hello World”应用程序是否会在服务器重新启动时启动时,我使用了 PM2 文档中建议的ecosystem.config.js。这称为像这样的javascript文件。

module.exports = {
    apps: [
      {
        name: "HelloWorld",
        script: "apps\\hello_world.js",
        instances: 1
      }
    ]
  }

这工作得很好,但是我找不到任何关于如何从这个文件运行npm start 的文档(尽管我确实尝试调用一个单独的批处理文件,但这也不起作用)。然后我看到here 我可以调用一个记录更多选项的 JSON 文件。

JSON 配置文件:

{
    "apps": [
        {
            "name": "docs",
            "cwd": "apps\\docs",
            "script": "npm",
            "args": "start"
        },
        {
            "name": "HelloWorld",
            "script": "apps\\hello_world.js"
        }
    ]
}

“HelloWorld”脚本启动,但文档应用程序未启动。

pm2 报告提供以下输出:

PM2        | 2020-01-07T17:54:20: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:20: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:20: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:21: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] starting in -fork mode-
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] online
PM2        | 2020-01-07T17:54:22: PM2 log: App [docs:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-07T17:54:22: PM2 log: Script C:\PROGRAM FILES\NODEJS\NPM.CMD had too many unstable restarts (16). Stopped. "errored"

这让我想到了 GitHub 上的 this 问题。不过,关于这些问题的活动似乎并不多,而且历史上也有过此类事件。

我认为我在使用 PM2 时可能做错了,或者它存在问题,因为我也无法通过命令行使用 PM2 启动文档。

C:\Node JS\apps\docs>pm2 start npm --name "docs" -- start

有没有人对我如何让它工作有任何想法或尝试过类似的事情?我会接受以下答案:

  • 在 Windows Server 上运行 Docusaurus 的另一种方法。
  • 使用 PM2 的替代方法。
  • 任何其他想法。

抱歉,如果我跑题了。

编辑:

pm2 start ecosystem.json --only docs的输出

C:\Node JS>pm2 start ecosystem.json --only docs
[PM2][WARN] Applications docs not running, starting...
[PM2][ERROR] Process failed to launch EPERM: operation not permitted, open 'C:\etc\.pm2\logs\docs-out.log'

它引用的日志是空的。 docs-error.log 反复重复同样的错误,但从昨晚开始就没有更新过。它可能是我在 JSON 文件中尝试不同语法创建的,但还是发布了它。

SyntaxError: Unexpected token ':'
    at Module._compile (internal/modules/cjs/loader.js:895:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Object.<anonymous> (C:\Users\user\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:27:21)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
:: Created by npm, please don't edit manually.

【问题讨论】:

  • pm2 start ecosystem.config.js --only docs 的输出是什么?
  • 嗨瓦希德。根据要求添加了额外的输出。
  • 如果不赞成投票的人评论他们为什么这样做会很好。直到昨天我才接触到这些技术,我认为这个问题表明了学习的意愿。
  • 如果目标是Windows平台,则需要run as administartor命令行。因为你的文件在C盘,而你当前的用户没有足够的权限。
  • 嗨 Vahid,恐怕这是来自提升的管理员提示。

标签: node.js npm pm2 docusaurus


【解决方案1】:

你的问题解决了吗?

我在macos下运行这个没问题,pm2版本是5.1.0

请尝试:在终端上运行“npm start”

  1. 如果命令执行并返回提示。那么你不能以你的方式使用 pm2 运行。你应该试试其他方法。
  2. 如果“npm start”不返回提示。那你就可以了。

【讨论】:

  • 嗨@JamesLiao,感谢您的提醒。我确实解决了这个问题,这个问题是特定于 Windows 的 - 我将更新我的答案。
【解决方案2】:

这有点干净和最新的答案:

package.json 文件(包含以下示例脚本)

"scripts": {
    "start": "concurrently npm:server npm:dev",
    "dev": "react-scripts start",
    "build": "node ./scripts/build.js",
    "eject": "react-scripts eject",
    "lint": "eslint src server",
    "shivkumarscript": "ts-node -T -P server/tsconfig.json server/index.ts"
  }

假设我们要使用 pm2 实用程序运行名为 'shivkumarscript' 的脚本。因此,我们的 pm2 配置文件应该如下所示,包含值为 'npm' 的 'script' 键和值为 'run' 的 'args' 键。在我们的例子中,脚本名称是 'shivkumarscript'。

ecosystem.config.json 文件

module.exports = {
    apps: [
        {
            name: "NodeServer",
            script: "npm",
            automation: false,
            args: "run shivkumarscript",
            env: {
                NODE_ENV: "development"
            },
            env_production: {
                NODE_ENV: "production"
            }
        }
    ]
}

假设你已经在你的机器上安装了 Node.js、NPM 和 PM2。然后下面应该是通过 pm2 启动应用程序的命令,该命令将依次运行 npm 脚本(在应用程序的 package.json 文件中提到的命令行):

对于生产环境:

pm2 start ecosystem.config.js --env production --only NodeServer

对于开发环境:

pm2 start ecosystem.config.js --only NodeServer

...还有 Boom!伙计们

【讨论】:

  • 你误解了这个问题,它不仅仅是能够通过 pm2 运行脚本。这是在 Windows 上通过 pm2 运行 npm 的特定问题。您不能通过系统路径直接调用 npm,因为它调用的是批处理文件而不是 npm js 脚本。
  • @Ash 您能否提供有关 app\docs 文件的更多详细信息?您还可以使用 npm 命令在终端中正常运行文档吗?
  • 我很久以前就已经发布了解决方案。一切都特定于从 Windows 上的 pm2 配置文件运行。
【解决方案3】:

问题实际上是 pm2 如何通过 Windows 上的批处理文件调用节点。如果您使用完整路径直接调用npm-cli.js,程序将毫无问题地运行。

{
    "apps": [
        {
            "name": "docs",
            "cwd": "some_path_to\\apps\\docs",
            "script": "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
            "args": "start",
            "exec_mode": "cluster",
            "instances": 1,
            "interpreter": "none"
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    相关资源
    最近更新 更多