0

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

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

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

这工作得很好,但我找不到任何关于如何npm start从这个文件运行的文档(尽管我确实尝试调用一个单独的批处理文件,但这也不起作用)。然后我在这里看到我可以调用一个记录更多选项的 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 上遇到了这个问题。不过,在这些问题上似乎没有很多活动,而且也有过历史性的事件。

我认为我可能在 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.
4

3 回答 3

0

问题实际上是 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"
        }
    ]
}
于 2020-01-10T16:43:21.553 回答
0

你解决你的问题了吗?

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

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

  1. 如果命令执行并返回提示。那么你不能以你的方式使用 pm2 运行。你应该尝试其他方式。
  2. 如果“npm start”不返回提示。那么你也能。
于 2021-06-24T02:13:58.370 回答
0

这是更清洁和最新的答案:

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'。

生态系统.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

......和Boooom!伙计们

于 2021-08-11T20:33:21.800 回答