如何调试Node.JS子分叉进程?

时间:2022-11-03 17:00:51

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

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

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

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

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

debugger listening on port 40893
debugger listening on port 40893

So it only debugs the parent process.

所以它只调试父进程。

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?

有没有办法设置IntelliJ来调试子进程或强制它在另一个端口上启动所以我可以在远程调试中连接它?

5 个解决方案

#1


19  

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

这是node.js中已知的最近修复过的错误(尽管没有向后移植到v0.10)。

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

有关更多详细信息,请参阅此问题:https://github.com/joyent/node/issues/5318

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:

有一种解决方法可以改变每个工作进程的命令行,尽管API并不是以这种方式使用(解决方法可能会在将来停止工作)。以下是github问题的源代码:

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);
}

#2


24  

Yes. You have to spawn your process in a new port. There is a workaround to debug with clusters, in the same way you can do:

是。您必须在新端口中生成进程。有一种解决方法可以使用集群进行调试,方法与您一样:

Start your app with the --debug command and then:

使用--debug命令启动应用程序,然后:

var child = require('child_process');
var debug = typeof v8debug === 'object';
if (debug) {   
    //Set an unused port number.    
    process.execArgv.push('--debug=' + (40894));    
}    
child.fork(__dirname + '/task.js');

debugger listening on port 40894

调试器侦听端口40894

#3


3  

Quick simple fix ( where using chrome://inspect/#devices )

快速简单修复(使用chrome:// inspect /#devices)

var child = require('child_process');
child.fork(__dirname + '/task.js',[],{execArgv:['--inspect-brk']});

Then run your app without any --inspect-brk and the main process won't debug but the forked process will and no conflicts.

然后在没有任何--inspect-brk的情况下运行您的应用程序,主进程将不会调试,但分叉进程将没有冲突。

To stop a fork conflicting when debugging the main process ;

调试主进程时停止fork冲突;

child.fork(__dirname + '/task.js',[],{execArgv:['--inspect=xxxx']});

where xxxx is some port not being used for debugging the main process. Though I haven't managed to easily connect to both at the same time in the debugger even though it reports as listening.

其中xxxx是某个未用于调试主进程的端口。虽然我没有设法在调试器中同时轻松连接到两者,即使它报告为监听。

#4


2  

if "process.execArgv" doenst work you have to try:

如果“process.execArgv”工作,你必须尝试:

if (debug) {
    process.argv.push('--debug=' + (40894));
}

this worked for me..

这对我有用..

#5


1  

There are one more modern way to debug child (or any) process with Chrome DevTools.

还有一种使用Chrome DevTools调试子(或任何)进程的现代方法。

Start your app with arg

用arg启动你的应用程序

--inspect

like below:

如下:

node --debug=9200 --inspect app/main.js

You will see the message with URL for each child process:

您将看到每个子进程的URL消息:

Debugger listening on port 9200.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9200/207f2ab6-5700-4fc5-b6d3-c49a4b34a311
Debugger listening on port 9201.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9201/97be3351-2ea1-4541-b744-e720188bacfa
Debugger listening on port 9202.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9202/8eb8384a-7167-40e9-911a-5a8b902bb8c9

If you want to debug the remote processes, just change the address 127.0.0.1 to your own.

如果要调试远程进程,只需将地址127.0.0.1更改为您自己的地址。

#1


19  

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

这是node.js中已知的最近修复过的错误(尽管没有向后移植到v0.10)。

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

有关更多详细信息,请参阅此问题:https://github.com/joyent/node/issues/5318

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:

有一种解决方法可以改变每个工作进程的命令行,尽管API并不是以这种方式使用(解决方法可能会在将来停止工作)。以下是github问题的源代码:

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);
}

#2


24  

Yes. You have to spawn your process in a new port. There is a workaround to debug with clusters, in the same way you can do:

是。您必须在新端口中生成进程。有一种解决方法可以使用集群进行调试,方法与您一样:

Start your app with the --debug command and then:

使用--debug命令启动应用程序,然后:

var child = require('child_process');
var debug = typeof v8debug === 'object';
if (debug) {   
    //Set an unused port number.    
    process.execArgv.push('--debug=' + (40894));    
}    
child.fork(__dirname + '/task.js');

debugger listening on port 40894

调试器侦听端口40894

#3


3  

Quick simple fix ( where using chrome://inspect/#devices )

快速简单修复(使用chrome:// inspect /#devices)

var child = require('child_process');
child.fork(__dirname + '/task.js',[],{execArgv:['--inspect-brk']});

Then run your app without any --inspect-brk and the main process won't debug but the forked process will and no conflicts.

然后在没有任何--inspect-brk的情况下运行您的应用程序,主进程将不会调试,但分叉进程将没有冲突。

To stop a fork conflicting when debugging the main process ;

调试主进程时停止fork冲突;

child.fork(__dirname + '/task.js',[],{execArgv:['--inspect=xxxx']});

where xxxx is some port not being used for debugging the main process. Though I haven't managed to easily connect to both at the same time in the debugger even though it reports as listening.

其中xxxx是某个未用于调试主进程的端口。虽然我没有设法在调试器中同时轻松连接到两者,即使它报告为监听。

#4


2  

if "process.execArgv" doenst work you have to try:

如果“process.execArgv”工作,你必须尝试:

if (debug) {
    process.argv.push('--debug=' + (40894));
}

this worked for me..

这对我有用..

#5


1  

There are one more modern way to debug child (or any) process with Chrome DevTools.

还有一种使用Chrome DevTools调试子(或任何)进程的现代方法。

Start your app with arg

用arg启动你的应用程序

--inspect

like below:

如下:

node --debug=9200 --inspect app/main.js

You will see the message with URL for each child process:

您将看到每个子进程的URL消息:

Debugger listening on port 9200.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9200/207f2ab6-5700-4fc5-b6d3-c49a4b34a311
Debugger listening on port 9201.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9201/97be3351-2ea1-4541-b744-e720188bacfa
Debugger listening on port 9202.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9202/8eb8384a-7167-40e9-911a-5a8b902bb8c9

If you want to debug the remote processes, just change the address 127.0.0.1 to your own.

如果要调试远程进程,只需将地址127.0.0.1更改为您自己的地址。