7
votes

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?

3
Maybe you can check inside myscript if the main process is in debug mode, and then pass --debug as one of the args. - loganfsmyth
and don't forget to change debugger port as default is already busy with parent process debugger - Andrey Sidorov
adding --debug, or --debug=5859 (a new port) to fork cmd or args results in errors. I think this is because --debug is an option to the Node process, not the process that is being forked. Any other ideas? - user836003

3 Answers

3
votes

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

0
votes

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 =)

-1
votes

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