简体   繁体   中英

Use Node.js command line debugger on child process?

I use the Node client debugger, like so:

node debug myscript.js

But this process spawns a child using:

var child = require("child_process").fork(cmd, args);

Is there a way for this child to ALSO be started in "debug" mode?

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:

var debug = process.execArgv.indexOf('--debug') !== -1;
if(debug) {
    //Set an unused port number.
    process.execArgv.push('--debug=' + (5859));
}
var child = require("child_process").fork(cmd, args);

.... debugger listening on port 5859

Not a problem. All you need to do is to send the debugging Sig to the running process. Look at the node-inspector docs do a find for Enable debug mode

I can`t tell you how to do a debug-brk, I am not sure you can but you can always do something from code like

while(true){debugger}

So you can catch the debug statement then step out of the loop manually. Dirty I know =)

Here's another way, this will make the child debuggable on a free port:

// Determine if in debug mode. 
// If so, pass in a debug-brk option manually, without specifying port.
var startOpts = {};
var isInDebugMode = typeof v8debug === 'object';
if(isInDebugMode) {
     startOpts = {execArgv: ['--debug-brk']};
}
child_process.fork('./some_module.js', startArgs, startOpts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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