【问题标题】:PM2 couldn't start the app and the app ends up in "errored" statusPM2 无法启动应用程序,应用程序最终处于“错误”状态
【发布时间】:2021-06-20 05:19:37
【问题描述】:

我在生产环境中使用 Windows Server 2012 R2 上的 Node/Express.js 服务器和 PM2.js 来使应用程序永远保持活动状态并在不停机的情况下重新加载它们。由于 PM2 在 Windows 操作系统上存在限制,因此我选择了 PM2 安装软件来克服这些限制。

当我使用 Node 运行时,服务器运行良好,因此服务器脚本没有问题。

node index.js

但是,当我使用 PM2 启动服务器时,服务器没有启动并且状态为“错误”(我之前也尝试过生态系统文件和集群模式,但没有成功)。

pm2 start index.js

日志文件是空的,所以没有线索。有没有人遇到过这个?有什么解决办法吗?

更新 1: 我在 C:\ProgramData\pm2\service

的 service.log 中注意到以下错误
2021-03-23T23:13:56: PM2 log: App [server-plugin:0] starting in -fork mode-
2021-03-23T23:13:56: PM2 log: App [server-plugin:0] online
ERROR: 2021-03-23T23:13:56: PM2 error: Error: spawn node ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
    at onErrorNT (internal/child_process.js:465:16)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
3/23/2021, 11:13:56 PM: default/server-plugin@N/A - start - MANUAL
ERROR: 2021-03-23T23:13:56: PM2 error: Cancelling versioning data parsing

该错误似乎表明它无法生成 node.js,但不确定原因。

更新 2: index.js 是直接在根文件夹中的小型测试应用程序,仅包含以下内容:

const fs = require('fs');
const express = require('express'),
     https = require('https'),
     
const config = require('./config');

const hostname = config.hostname;
const port = config.port;

const app = express();

app.get("/*",(req, res, next) => {
  console.log(req.headers);
  res.send(`<html><body><h1>Hello ${req.params[0] || 'World'} from ${port} port!</h1></body></html>`);

});

https.createServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, app).listen(port, hostname, () => {
  console.log(`Server running at https://${hostname}:${port}`);

根文件夹中有一个随附的 config.js、SSL 文件和 node_modules 子文件夹。就是这样。

【问题讨论】:

  • 你的快递应用有“bin”目录吗?你可以用 pm2 start bin/www 运行它
  • 定位 index.js 不是问题,因为我尝试过使用相对或绝对路径,但我得到了同样的错误。我也尝试过其他快递应用程序。这个问题大家都一致。
  • @Ravindra 似乎你不明白。请向我们展示index.js 文件的上下文或再次检查,您的项目根目录是否有bin 文件夹,如果有,则开始。 www 文件,而不是 index.js
  • 在描述中添加了关于 index.js 的信息。即使使用这个小型测试应用程序,我也可以重现该问题。
  • 最近自己通过pm2查看过。如果应用程序直接通过node index.js 命令启动,则尝试通过PM2 生态系统文件或cluster 选项启动它,而不是fork。否则,问题可能出在fs 模块和 pm2 对其的访问中,因为您可以在直接启动它时访问它们,但作为服务的 pm2 对此目录没有足够的权限。我以前见过这个:stackoverflow.com/questions/66739573/…

标签: node.js express pm2


【解决方案1】:

我的解决方案是放弃pm2-installer 并使用任务调度程序方法。这个solution 是救世主。它就像一个魅力。另一个技巧是将环境变量 PM2_HOME 设置为 c:\users\&lt;user&gt;\.pm2 或类似的东西。这将有助于PM2 resurrect 从该位置获取转储文件。还要重启机器以使该方案工作,但如果无法重启,请手动运行创建的计划任务。

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多