interfect
interfect

Reputation: 2877

How do I use the `node inspect` debugger on an application that uses standard input?

I'm trying to track down the source of an error in a Node application that doesn't come with a stack trace.

The node inspect debugger supports a breakOnException command that I think will help me.

But my application needs to take input from standard input before it produces the error I am looking for, and node inspect seems to leave my standard input going to a persistent debug> prompt even afrer I continue, when the application being debugged is running and not paused. This is unlike gdb, where when you continue the debugger prompt goes away and you need to pause the application with Ctrl+C to get it back.

How do I make node inspect's prompt go away so I can type input into the application being debugged? Alternately, if I run the application under node --inspect for remote debugging, how can I connect to a remote debugging session on the command line?

I know that a browser or an IDE like VSCode can connect to a remote NodeJS inspection session, but I can't seem to find a way to connect with the command-line debugger that is built in to node.

Upvotes: 1

Views: 2194

Answers (1)

interfect
interfect

Reputation: 2877

node inspect --help doesn't document this at all, but if you launch one Node process with node --inspect, you can attach the built-in CLI debugger using the secret -p <PID> option. Someone on Medium figured this out, and did not give a source. It's also mentioned in this answer about debugging servers.

So, start your app:

$ node --inspect whatever.js arg arg arg
Debugger listening on ws://127.0.0.1:9229/47219dfa-1b9b-40f8-92a9-0fc670bb2e0b
...

Then work out its PID:

$ pgrep -f "node --inspect"
12345

And then attach:

$ node inspect -p 12345
connecting to 127.0.0.1:9229 ... ok
debug>

Upvotes: 1

Related Questions