Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot debug node child process (using node native debugger) #8690

Closed
yanivefraim opened this issue Sep 21, 2016 · 8 comments
Closed

cannot debug node child process (using node native debugger) #8690

yanivefraim opened this issue Sep 21, 2016 · 8 comments
Labels
inspector Issues and PRs related to the V8 inspector protocol question Issues that look for answers.

Comments

@yanivefraim
Copy link

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?

@bnoordhuis
Copy link
Member

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.

@bnoordhuis bnoordhuis added question Issues that look for answers. inspector Issues and PRs related to the V8 inspector protocol labels Sep 21, 2016
@yanivefraim
Copy link
Author

yanivefraim commented Sep 21, 2016

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

@bnoordhuis
Copy link
Member

bnoordhuis commented Sep 21, 2016

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

@yanivefraim
Copy link
Author

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!

@bnoordhuis
Copy link
Member

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

@quyetvv
Copy link

quyetvv commented Oct 10, 2017

@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?

@yanivefraim
Copy link
Author

@medikoo
Copy link

medikoo commented Oct 10, 2017

Related issue: #9435 (prematurely closed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inspector Issues and PRs related to the V8 inspector protocol question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants