7

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. Commented Mar 1, 2012 at 1:20
  • and don't forget to change debugger port as default is already busy with parent process debugger Commented Mar 1, 2012 at 3:16
  • 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? Commented Mar 2, 2012 at 1:36

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I struggled on this because I affected the child_process module to a local variable called process, hiding the node process global variable, hence process.execArgv was not defined. Beaware "process" is a node global variable!
0

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

Comments

-1

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

1 Comment

Sorry this didn't work for me, I think you need to explicitly tell it to listen on an unused port, like so execArgv.push('--debug=' + (5303 + processId++));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.