How to develop a child process that listens on a different debug port than the parent

I am trying to use child_process.fork to create a process that interrupts and listens for the V8 debugging protocol.

However, I cannot force the forked process to listen on a port other than the parent process. Assuming the parent process is listening on 6000 , the child process is also trying to listen on port 6000 :

Could not open socket on port 6000, waiting for 1000 ms before retrying

Here is my code:

 // `test.js`, invoked as `node --debug-brk=6000 test.js` var nodeModule, args, env, child nodeModule = path.normalize(path.join(__dirname, '..', 'app.js')) args = [ '--debug-brk=6001' , '127.0.0.1' , 3030 , 'api-testing' ] env = { 'DB_URI': 'mongodb://localhost/test' } child = require('child_process') .fork(nodeModule, args, {env: env}) .on('message', callback) 

As you can see, I am trying to listen to the forked process on port 6001 , but the child process is trying to listen to port 6000 , which is used by the parent.

How can I make a child process listen on port 6001 or some other free port?

There are several topics on this subject. For example:

  • How to debug a Node.JS child forked process?
  • Debugging Node.js processes using cluster.fork ()

But:

  • These threads are for cluster forking.
  • Refer to execArgv , which looks like it was undocumented for process and still not documented for cluster .
+8
source share
2 answers

A fairly simple answer found on this comment and with some help from # Node.js on Freenode:

Just move --debug-brk to the execArgv key of the options parameter to fork :

 // Excerpt: args = [ '127.0.0.1' , 3030 , 'api-testing' ] env = { 'DB_URI': 'mongodb://localhost/test' } child = fork(nodeModule, args, { env: env , execArgv: ['--debug-brk=6001'] }) .on('message', this.callback) 

execArgv is an array of parameters passed to the node process. argv is the collection passed to the main module. There the child_process.fork parameter for argv highlighted, but execArgv should be placed in the opts parameter. This works, and in the child process we have:

 > process.execArgv ["--debug-brk=6001"] > process.argv ["/usr/local/Cellar/node/0.10.13/bin/node" "/Users/dmitry/dev/linksmotif/app.js", "127.0.0.1", "3030", "api-testing"] 

Finally

Node.js sequentially considers execArgv and argv as separate sets of values.

+8
source share

Before using fork, remove the old debug-brk parameter:

 process.execArgv = process.execArgv.filter(function(o){ var x = "--debug-brk"; return o.substring(0, x.length) !== x }); 

and add a new one:

 process.execArgv.push("--debug-brk=9999"); 
0
source share

All Articles