Node: cannot debug node child process (using node native debugger)

Created on 21 Sep 2016  路  8Comments  路  Source: nodejs/node

I'm trying to debug node child process, using native node debugger. See this repo for example.

I tried all king of options, according to: debug1, debug1, debug3 (and a lot of other references I found online).

Non of those options worked for me..

This is my example code:

index.js:

const spawn = require('child_process').spawn;
const path = require('path');

const ls = spawn('node', [path.resolve('./child.js')], {execArgv: '--debug-brk=4545'});

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

child.js:

debugger;
const a = 123;

console.log(a);

I then run:

node --debug-brk --inspect=9222 index.js

And I open the chrome-devtools://devtools/... in chrome. Debugging the main process works great, and I see the child process output as well. Only thing that is not working is debug of child process...

What am I doing wrong here?

inspector question

Most helpful comment

That doesn't work for spawn(), you may be thinking of fork(). Explicitly pass --inspect=... as an argument and it should work, i.e.:

const file = path.resolve('./child.js');
const args = ['--inspect=9223', file];
const proc = cp.spawn(process.execPath, args);

All 8 comments

If you are spawning child processes, you need to pass --inspect=<port> to them and give each child process its own unique port number. You will need to open a separate devtools instance for each process.

Thanks @bnoordhuis - i tried that without success, maybe I'm doing something wrong?

In my example, I tried (inside index.js):

process.execArgv.push('--inspect=' + (9223));

const ls = child.spawn('node', [path.resolve('./child.js')]);

I then opened both child and index devtools (child first and then index). I was expecting to hit breakpoints on child while running index.js, but it didn't work

That doesn't work for spawn(), you may be thinking of fork(). Explicitly pass --inspect=... as an argument and it should work, i.e.:

const file = path.resolve('./child.js');
const args = ['--inspect=9223', file];
const proc = cp.spawn(process.execPath, args);

Thanks! This is working now. I really think we should add docs for debugging child processes/cluster to the node debug section. This can be really tricky and documentation almost doesn't exists!

You can open a documentation pull request if you want, the files to look for are in doc/api.

@yanivefraim : I follow the suggestion but it is not working at all. Here is my code for start spawn process:

server = cp.spawn('node', [serverPath, '--inspect=9223'], {
env: Object.assign({ NODE_ENV: 'development' }, process.env),
silent: false,
});

Could you help? Is there anything wrong here?

Related issue: https://github.com/nodejs/node/issues/9435 (prematurely closed)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

jmichae3 picture jmichae3  路  3Comments