3

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?

Community
  • 1
  • 1
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96

1 Answers1

6

Answering my own question, just in case someone will have the same problem as I did. According to this issue, the solution is:

const file = path.resolve('./child.js');
const args = ['--inspect=9228', '--debug-brk', file];
const ls = spawn('node', args);

You will have to open chrome-devtools://devtools/... with 9222 port + a new inspect window for child process, on port 9228 (as this is the port I added in this example)

You can see a working example here

Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96