12 Replies Last post: Jan 14, 2014 4:31 PM by Sergey Simonchik  
ryan h Newbie 29 posts since
Jan 31, 2013
Currently Being Moderated

Jan 7, 2014 7:33 PM

Debugging node child processes

Hi

I have a nodejs server .

From the server I Fork a new child process (node also that points to a js file.

 

If I run my server in debug mode console.log and breakpoints do not work in the forked process.

If I run (not in debug) then the console.log does work on the forked process.

 

I want to debug my forked process and put breakpoints  how can I do it.

Thanks.

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 13, 2014 2:12 PM in response to: ryan h
Re: Debugging node child processes

Hi,

 

Looks like you need to set "execArgv" parameter to empty array. When a node process is debugged it's started with --debug-brk=<port> argument. So all forked node processes inherit this argument, but it isn't desired behavior, because the port is already bound by the parent node process.

 

Here is an example that shows how that could be accomplished:

 

main.js

var child_process = require('child_process')
  , path = require('path');

child_process.fork(
  path.join(__dirname, 'child.js'),
  [],
  {
    execArgv: []
  }
);

setInterval(function () {
  console.log('Hello from parent');
}, 2000);

 

 

child.js

console.log('Child process started');

setInterval(function () {
  console.log("Hello from child process");
}, 1000);

 

Hope that will help.

 

Sergey

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 13, 2014 4:17 PM in response to: ryan h
Re: Debugging node child processes

Forgot about breakpoints. Sorry.

 

If you'd like to debug child process, you need to pass "--debug-brk=<any free port>" as "execArgv" parameter to it.

With "execArgv: []" child process is obviously started in a run mode.

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 13, 2014 4:22 PM in response to: ryan h
Re: Debugging node child processes

See also about debugging child process: http://youtrack.jetbrains.com/issue/WEB-1919

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 13, 2014 6:54 PM in response to: ryan h
Re: Debugging node child processes

Please file an issue in the http://youtrack.jetbrains.com/issues/WEB#newissue=yes (subsystem: Debugger).

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 13, 2014 6:53 PM in response to: ryan h
Re: Debugging node child processes

1. Is there any way to debug both parent and child in the same session ?

 

Yes. Just make sure "--debug-brk=<free port>" is passed to a forked child node process.

Also make sure you're using WebStorm 7.0.3. (WebStorm 7.0.2 doesn't debug child node processes correctly).

Click "Debug" button (that starts parent node process debugging) and it should work in the following way:

* A console associated with the parent node process will shown up.

* As soon as a child node process is forked, there will be automatically created "Node.js Remote Debug" run configuration for the child node process. And it will be shown near parent's console.

2. is this the only way , it looks like some kind of workaround   var childProc= fork('./public/LG/lg.js',{execArgv: ['--debug-brk=5858']});  ..... When I finish dev I must remove the exectArgv no ? . This doesnt seem like code to keep in the production . I couldnt find the --debug-brk in node js documentation)  ?

 

Oh, it could be simplified in the following way (forgot to mention it in the first place):

var childProc= fork('./public/LG/lg.js',{execArgv: ['--debug-brk']})

In that case node automatically finds a free port.

 

But yes, you need to keep some logic whether a child process should start in a debug mode or not.

For example, possible logic could be like this: start a child process in a debug mode if a parent process has been started in a debug mode.

To verify that a node process is started in a debug mode, you can analyze "process.argv" array.

FYI, "cluster" node core modules works in this way.

 

Can't find any links about "--debug-brk" in node documentation either.

3. I see that once I do this , there are no console outputs from the child process ?

Is child process actually running?

If a node process is started with "--debug-brk", it's suspended on the first line.

It continues running after starting "Node.js Remote Debug" run configuration with debug port of this process.

So make sure you start "Node.js Remote Debug" run configuration.

 

 

You can take a sample code from my first comment, replace

execArgv: []

with

execArgv: ["--debug-brk"]

and hit "Debug" main.js.

After that you'll see output of both processes in console associated with the parent process and breakpoints in child.js should work also.

 

Please let me know whether it's working for you.

Sergey Simonchik JetBrains 175 posts since
Sep 28, 2011
Currently Being Moderated
Jan 14, 2014 4:31 PM in response to: ryan h
Re: Debugging node child processes

Great!

BTW I am using IDEA 12.1.4.   Are there any known debugging issues ?

Well, a lot of debugging issues have been fixed since IDEA 12.1.4: http://youtrack.jetbrains.com/issues/WEB?q=%23Fixed+Subsystem%3A+Debugger+Fix+versions%3A+7*

But I'm not sure whether your issues is fixed or not...

I'd recommend you to try the latest IDEA 13 release and reproduce the issue there.

Since no new IDEA 12.1 releases are planned, only issues that are reproducible with IDEA 13 will be addressed.

Thanks.

More Like This

  • Retrieving data ...