简体   繁体   English

无法使用pm2启动节点进程

[英]Can not start node process using pm2

I'm new to node.js and having difficulty to demonize this app using pm2. 我是node.js的新手,很难使用pm2来演示应用程序。 The app works fine when I go to /srv/myapp and run yarn start but when I try to demonize it by going to /srv/myapp and run pm2 start index.js 当我转到/srv/myapp并运行yarn start时,该应用程序运行良好,但是当我尝试通过转到/srv/myapp并运行pm2 start index.js进行妖魔化时,

I see that the process seem to have started: 我看到该过程似乎已经开始:

[PM2] Starting /srv/myapp/index.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ index    │ 0  │ fork │ 14528 │ online │ 0       │ 0s     │ 0%  │ 16.0 MB   │ root │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

But the front end does not respond (instead I get 502 errors and the node server does not listen on port 3000 as expected ( fuser 3000/tcp returns no results). 但是前端没有响应(相反,我收到502 errors ,并且节点服务器未按预期方式在端口3000上侦听( fuser 3000/tcp返回任何结果)。

What could be wrong here? 这有什么问题吗? How can I fix it? 我该如何解决?

通过命令检查pm2日志: pm2 logs如果没有错误,则说明您的应用程序没有问题。

pm2 starts your app with node, one problem is it doesn't provide any logging or output like starting an app by other means (such as node index.js does), so it doesn't really let you know if there is anything going wrong. pm2使用节点启动您的应用程序,一个问题是它不提供任何日志记录或输出,就像通过其他方式启动应用程序一样(例如, node index.js这样做),因此它并没有真正让您知道是否正在发生任何事情错误。

I've always found it's a good idea to test your app by starting it with node before pm2 . 我一直发现,通过从pm2之前的node启动它来测试您的应用程序是个好主意。

Given node index.js causes the error: import bb from 'bluebird';SyntaxError: Unexpected token import , which makes it sound like you're using ES6 import feature, not supported by Node by default . 给定node index.js会导致错误: import bb from 'bluebird';SyntaxError: Unexpected token import ,这听起来像您正在使用ES6导入功能, 默认情况下Node不支持 I suspect Yarn is either enabling these features, or transpiling your ES6 code to ES5 for Node to run when you do the yard start , but I'm not familiar with Yarn to know. 我怀疑Yarn是启用这些功能,还是将您的ES6代码转换为ES5以便在执行yard start时可以运行Node,但是我对Yarn并不了解。

It may be you're pointing at the wrong file, and there's a transpiled file dist.js (as an example) already created that Yarn is happily running with the opaque start command. 可能是您指向错误的文件,并且已经创建了一个已编译的文件dist.js (作为示例),Yarn正在使用不透明的start命令愉快地运行。


Edit: After looking at the source, it looks to be using something called babel-node . 编辑:查看源代码后,似乎正在使用称为babel-node东西 See the start script in the package.json : 请参阅package.json中启动脚本

"scripts": {
  "start": "babel-node index.js",

pm2 doesn't know to run your index.js with this babel-node thing, and even if it did babel-node strongly recommends you do not use babel-node in production (which presumably you are doing with pm2 ): pm2不知道如何使用babel-node来运行index.js,即使它做到了,babel-node强烈建议您不要在生产中使用babel-node(大概是在使用pm2 ):

Not meant for production use You should not be using babel-node in production. 不适用于生产用途您不应在生产中使用babel-node。 It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. 由于缓存存储在内存中,因此不必要地增加了内存使用量。 You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly. 由于整个应用程序都需要动态编译,因此您还将始终遭受启动性能损失。

Check out the example Node.js server with Babel for an idea of how to use Babel in a production deployment. 查看带有Babel的示例Node.js服务器,以了解如何在生产部署中使用Babel。

And there's also a note about ES6 module loading: 还有关于ES6模块加载的说明:

ES6-style module-loading may not function as expected Due to technical limitations ES6-style module-loading is not fully supported in >a babel-node REPL. ES6样式的模块加载可能无法按预期方式运行由于技术限制,babel-node REPL中不完全支持ES6样式的模块加载。

I'd strongly recommend you add a build script to produce a 'distribution' transpiled version with babel and deploy that! 我强烈建议您添加一个build脚本,以使用babel生成一个“ distribution”转译版本并进行部署!

"scripts": {
  "start": "babel-node index.js",
  "build": "babel index.js --out-file index-dist.js

Or something along those lines, and run yarn build (or yarn run build ?) then pm2 start index-dist.js . 或者沿着这些路线,运行yarn build (或yarn run build ?),然后pm2 start index-dist.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM