How is Visual Studio Code debugger able to attach to a already running node process?
I simply used npm start to run my node application and then use the VS Code "attach to process Id" launch configuration to start the debugger. I didn't had to start my application with --inspect or --debug flag. How is this actually working?
As can be found in the nodejs docs:
Node.js will also start listening for debugging messages if it receives a SIGUSR1 signal. (SIGUSR1 is not available on Windows.) In Node.js 7 and earlier, this activates the legacy Debugger API. In Node.js 8 and later, it will activate the Inspector API.
VS Code sends SIGUSR1 to the process. From the VS Code docs:
the debugger tries to attach to this process after having sent a USR1
signal. With this setting, the debugger can attach to an already
running process that was not started in debug mode.
Related
When I start an Express server with the --inspect option, I can navigate to chrome://inspect/#devices and launch a debug console for the appropriate Remote Target. But, when I start the process without the --inspect option, then try to initiate the inspector with kill -10 $PID, Chrome does not detect the Remote Target, even though the server indicates that the debugger was started. Is there anything I can do to connect to this process by Chrome or some other means?
This is the output from the server after running kill -10 $PID:
Attaching to nodejs
nodejs | Running version 3 on http://0.0.0.0:5000
nodejs | Debugger listening on ws://127.0.0.1:9229/c948d085-0491-4cd9-9832-a0f0cf120e11
nodejs | For help, see: https://nodejs.org/en/docs/inspector
Another note: I am running this app in a container with a Docker bridge network, but I don't see why that should make a difference because the --inspect option registers the same output when I start the server with it.
Versions:
Docker version 19.03.13, build 4484c46d9d
Node v12.22.1
express#4.17.1
Additional note: the procedure I'm describing (starting debug session after server start) is described here: nodejs.org/en/docs/guides/debugging-getting-started
But, when I start the process without the --inspect option, Chrome does not detect the Remote Target
If you started a process without --inspect it is expected that you won't be able to debug your code. Make sure that that logs you are talking about are the latest and some junk from past runs before you removed --inspect flag
I've heard a lot of rumors about being able to create a debug session on a running node application by passing SIGUSR1 to the application.
Meaning I don't have to start an application with node debug app.js in order to attach a debugger to it.
How do I send SIGUSR1 to my application and then attach a debug client to the process? It would be especially helpful if I could do this from within a Docker container with only node.
How to send SIGUSR1
Use kill -l to view a list of signals.
In my case USR1 corresponds to 10.
ps to find the process I want to send the signal to.
kill -10 <pid>
A debug session is now open.
Attaching the debugger.
node debug localhost:5858
5858 is the default port for the legacy debugger. In node8 sending USR1 will activate the new inspector protocol, but until then this is how you can start and connect the debugger on a live application.
Watch this space for updates: https://nodejs.org/en/docs/guides/debugging-getting-started/
I'm remote debugging a node.js process via WebStorm (running on MacOS 10.12.6).
I'm attaching to the process successfully (I know this because I run node with --debug-brk and then the process suspends until I resume it in WebStorm), however there is no indication that the breakpoints are attached (of course they also don't work - the code in the breakpoint line is running but the breakpoint isn't suspending the process).
Few notes:
The breakpoint is enabled and "Suspend" is checked.
Local debugging works fine
Don't know if it's related by I tried to switch this registry: js.debugger.v8.use.any.breakpoint on/off and there was no change.
This is the command I use to run the node:
node --debug-brk=5858 server.js
Anybody has an idea?
I debug my node applications by executing node debug app in my application folder.However if i want to use the same with other application at same time it shows me error.
How can i use debug with the other application too?
Node's default debug port is being used by the first process. Start your second app specifying another port using the --debug-brk or the --debug flag like this:
node --debug-brk=5859 app.js
Then open another terminal window and connect to the second process's debugging port using:
node debug localhost:5859
I am following the directions here: https://github.com/node-inspector/node-inspector
Pretty simple. npm install node-inspector. In one shell run node-inspector and in another run node --debug app.js. Shell spits out an localhost address with specific port for the debugger. Open that address in a browser — it loads your code. From there add breakpoints, debug network, etc..
But this does not work.
The following message endlessly logs in my shell:
Failed to open socket on port 5858, waiting 1000 ms before retrying
..and no connection is made.
So my question is has anyone had this problem and successfully found a solution. Would love to get this working, would be super useful. My only experience in server-side debuggers is Ruby's Byebug which was a piece of cake to set up.
The message you see is printed because there is another process listening on port 5858. Either a different application, or simply another instance of a node process under the debugger.
An update based on comments below this answer
If your application is running in a cluster mode via Node.js core module "cluster", then you end up with multiple worker processes, where each of them is trying to listen on port 5858.
Node Inspector cannot debug multiple worker processes at the same time. You have to pick one process and enable the debugger in that process only. When you start the app via node --debug, all processes try to enable the debugger, therefore you cannot use this command.
Instead, you have to:
Start your app without the debugger enabled.
Then you need to find the pid (process id) of the worker process you would like to debug.
Once you have the PID, you need to enable the debugger in the target process. You can run kill -1 <pid> on UNIX, or use node debug -p <pid> that works on all platforms. Don't forget to exit node debug before proceeding further.
Once the target process has debugger listening on 5858, you can start Node Inspector the usual way.
My answer is based on the following blog post: https://strongloop.com/strongblog/whats-new-nodejs-v0-12-debugging-clusters/, check it out for more details.