如何调试 Node.JS 子分支进程? [英] How to debug Node.JS child forked process?

查看:47
本文介绍了如何调试 Node.JS 子分支进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试使用以下方法创建的子 Node.JS 进程:

I'm trying to debug the child Node.JS process created using:

var child = require('child_process');
child .fork(__dirname + '/task.js');

问题是在 IntelliJ/WebStorm 中运行时,父进程和子进程都在同一个端口上启动.

The problem is that when running in IntelliJ/WebStorm both parent and child process start on the same port.

debugger listening on port 40893
debugger listening on port 40893

所以它只调试父进程.

有没有办法设置 IntelliJ 来调试子进程或强制它在不同的端口上启动,以便我可以在远程调试中连接它?

Is there any way to set IntelliJ to debug the child process or force it to start on a different port so I can connect it in Remote debug?

推荐答案

这是 node.js 中的一个已知错误,最近已修复(尽管未向后移植到 v0.10).

It is a known bug in node.js that has been recently fixed (although not backported to v0.10).

查看此问题了解更多详情:https://github.com/joyent/node/issues/5318

See this issue for more details: https://github.com/joyent/node/issues/5318

有一种变通方法,您可以更改每个工作进程的命令行,尽管 API 并不打算以这种方式使用(变通方法将来可能会停止工作).这是来自 github 问题的源代码:

There is a workaround where you alter the command-line for each worker process, although the API was not meant to be used this way (the workaround might stop working in the future). Here is the source code from the github issue:

var cluster = require('cluster');
var http = require('http');

if (cluster.isMaster) {
  var debug = process.execArgv.indexOf('--debug') !== -1;
  cluster.setupMaster({
    execArgv: process.execArgv.filter(function(s) { return s !== '--debug' })
  });
  for (var i = 0; i < 2; ++i) {
    if (debug) cluster.settings.execArgv.push('--debug=' + (5859 + i));
    cluster.fork();
    if (debug) cluster.settings.execArgv.pop();
  }
}
else {
  var server = http.createServer(function(req, res) {
    res.end('OK');
  });
  server.listen(8000);
}

这篇关于如何调试 Node.JS 子分支进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆