Start debug session on running node app and attach node debugger? - node.js

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/

Related

Chrome does not detect debugger if attached after process start

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

How VS Code debugger attaches to a node process

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.

How to debug two node applications at same time using node's built-in debugger?

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

Node-Inspector Failed to open socket

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.

Using Node Inspector with multiple node processes

I started using Node Inspector to debug some of my Node applications. However, one thing i am not sure how to do is, once Node-inspector is attached to one Node app, how to detach and attach it to another Node app running on same box?
How can I debug multiple processes at the same time?
Update:
If you are reading this in 2019, the below answer is out of date. You'd probably want to check out the current documentation or follow gtzilla answer:
https://nodejs.org/en/docs/guides/debugging-getting-started/
First, start your node programs with different debug ports like so:
$ node script1.js --debug==5858
$ node script2.js --debug==5859
Then start node-inspector
$ node-inspector &
and open the web console in two tabs with
http://localhost:8080/debug?port=5858
http://localhost:8080/debug?port=5859
As mentionned https://stackoverflow.com/a/18911247/1301197 you can specify a port with
node --inspect=7000 --inspect-brk app1.js
Then of course you just to specify a different port for each node server
node --inspect=7001 --inspect-brk app2.js
Attach the debugger
Either by port or by process id. For ports, use a different port for each process. On the command line:
node --inspect 8085 some_script_1.js
node --inspect 8086 some_script_2.js
node --inspect 9012 some_script_3.js
In a separate terminal window, you can attach to any of these processes with node inspect <host>:<port>. For example to attach to some_script_2.js on port 8086
node inspect 127.0.0.1:8086
Attaching to different processes is matter of changing the port, for example 9012 you would run
node inspect 127.0.0.1:9012
If you didn't start node on a separate, known port, you can also use the -p flag to attach directly to an existing process
node inspect -p <node_script_process_id>
On Linux and Mac OS use ps -A | grep node to find node process ids. Once a process is started, you can also attach the inspector by sending signal to the node process SIGUSR1 Reference
The node-inspect program (source) is separate from core node. Though it is bundled with nodejs. Node inspect reimplements node debug to address a limitation
For Chrome inspector protocol, there's only one: node --inspect ... This project tries to provide the missing second option by re-implementing node debug against the new protocol.
Debugger API documenation
Additional Ways to Attach Debugger
https://nodejs.org/en/docs/guides/debugging-getting-started/
You can view an interact with the debugger in Chrome. Just add additional connections under the Connections tab of the dedicated NodeJS DevTools window.
Similar, but Separate, Projects
Worth noting there is a similar project, now deprecated, that is called node-inspector, which is separate from node-inspect Tested October, 2018 with node v10.11.0
If you use Chrome, then you can also use devtools directly with url like:
devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/0cc79945-8386-4082-aabb-328341bfc953
*where 9229/0cc79945-8386-4082-aabb-328341bfc953 - part can be taken from node's output
For example, run first app with:
$ node --inspect-brk=7777 app.js
Debugger listening on ws://127.0.0.1:7777/2df21a01-44ff-40c4-b6ff-1f839f81f9d6
and so the result url will be:
devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:7777/2df21a01-44ff-40c4-b6ff-1f839f81f9d6
then run second app instance with:
$ node --inspect-brk=7778 app.js
Debugger listening on ws://127.0.0.1:7778/d4e8d8ce-abe9-46c6-89b1-ad0616bdf237
and open it with:
devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:7778/d4e8d8ce-abe9-46c6-89b1-ad0616bdf237

Resources