Debugging Kibana backend Node.js code - node.js

How to debug Kibana backend source code?
I presume, node-inspector could be used. And some extra configuration needed for package.json file to run debugger at npm start. But, I can't figure out the correct configuration syntax.

NODE_OPTIONS=--debug bin/kibana --dev
If you want to break before starting:
NODE_OPTIONS="--debug --debug-brk" bin/kibana --dev
Alternatively, you can set the same variable when calling npm start:
NODE_OPTIONS=--debug npm start
You will see:
Debugger listening on port 5858
You can then use the node inspector by running node-inspector and opening http://127.0.0.1:8080/debug?port=5858.
If you are debugging a remote server, you can either run node-inspector on the server and forward port 8080 through SSH, or you can run node-inspector locally and forward port 5858 through SSH.
EDIT: As it was now mentioned in the comments, it might be necessary to edit the source and add debugger; at the beginning, otherwise breakpoints might not work correctly even when using --debug-brk. Not sure why, though.

add $NODE_OPTIONS --no-warnings --inspect
and start kibana server and access the link shown in the logs like this link "chrome-devtools://devtools/remote/serve_file/#521e5b7e2b7cc66b4006a8a54cb9c4e57494a5ef/inspector.html?experiments=true&v8only=true&ws=localhost:5858/node" then you will find your backend code in the source tab and you can debug it like debuging frontend code.

Related

WebSockets request was expected error when using --inspect-brk option

When I run nodemon dist/server/app.js it works on default port 3000 and I'm able to reach my API. But if I run
nodemon --inspect-brk=localhost:3000 dist/server/app.js
I got error
WebSockets request was expected
What's wrong?
You can't run your web server and the debugger on the same port. They are each separate servers (the debugger is a server built into the node.js runtime).
So, you can either remove the port and host designation from the --inspect-brk option and just let it use the defaults (which is all I ever use) or you can select a different port for the debugger that doesn't conflict with your web server or anything else running on that host.

Change debug port for nodemon or mocha

I can use node-inspector to debug my nodemon app. But I want to also debug my tests. So I learnt that I need to start mocha in debug mode too (mocha --debug) problem is, this will try to debug on port 5858 by default too which in my case nodemon is using. So I will need to change the debug port of either, how do I do that?
Ok figured I can use mocha --debug=8101 to change mocha's debug port.
The same command line parameter works for nodemon and node. To bind on a different address, you can specify the IP like --debug=127.0.0.1:5858 (which is the default since Node.js v7), or use a wildcard --debug=[::]:5858 (which was the default in Node.js v6 and lower).

Node-inspector does not attach itself to application

I have tried launching node-inspector using:
node-inspector
node-inspector &
I have tried launching my app using:
node --debug ./bin/www
node --debug-brk ./bin/www
I am running the app from a vagrant box from which I have used port forwarding for the port 3000 and running the node-inspector on my windows PC.I have tried disabling all my Chrome extensions,I have tried removing and reinstalling node-inspector as well.I have tried adding debugger messages to my files.
I always get this window when I open node-inspector,it does not load any of the applications files for me to set breakpoints.Here is a screenshot of node-inspector.
I have also tried disabling my antivirus(Avast) and that does not work,I have tried forwarding the port 5858.
I have tried starting the default debugger,it just says connecting...:
node debug localhost:5858
How can I understand what the problem is and how can I solve it?
You need to forward port 5858 as well from your vagrant box to pc. Alternatively, start node-inspector inside vagrant and forward its web port ( 8080 by default )

How do you get node-inspector to work?

Install:
npm install node-inspector
Run app:
node --debug-brk c:\users\me\desktop\myapp.js // app stops successfully
Run inspector:
node-inspector // doesnt work.
C:\Users\me\node_modules\.bin\node-inspector // does work.
Open chrome or safari and make sure debugger is on and navigate to localhost:5858. But this releases the app from the breakpoint and it doesn't show up anywhere inside chrome or safari.
Solution:
Go directly to http://​localhost:8080.
Port 5858 is the port that node-inspector uses to communicate with your node process; you don't access it directly. It speaks V8's raw debugging protocol.
inspector talks to your process via that protocol and serves up a HTTP website on 8080.
Also, npm should have put C:\Users\me\node_modules\.bin\ in your PATH. You might need a reboot for that to take effect, after which you should be able to just type node-inspector at a command prompt (rather than typing the whole path).
You need to visit http://localhost:8080/debug?port=5850 instead of http://localhost:5858.
Visit http://localhost:8080/debug?port=5858 to open the node-inspector console.

How do you debug a Node.js server running with Chrome/WebKit as the remote debugger?

If you have your Node running
node --debug server.js
This gives me a port number xxxx, should I use this port number when starting Chrome?
Do you remote debug into it from Google\ Chrome --remote-debugging-port=xxxx?
Or is the 9222 a magic port, as it is mentioned all over.
Am I on the right track, trying to start Chrome with --remote-debugger into the Node.js server.js
The node-inspector / --debug are now replaced by inspector
See update below
#now deprecated / see below for update
#install node-inspector
npm install -g node-inspector
#start node-inspector, listen on port 8080 (default)
node-inspector --web-port=8080
#in another terminal session/window:
#while node-inspector is running, start your project in debug mode
node --debug myproject.js
Now you can browse to http://your_server:8080 for a full debug session of myproject.js
If your remote server is not accessible on the remote port because of firewalls or other reasons, you could create an ssh-tunnel to it from port 8080 on your local machine to 'localhost:8080' on the remote server:
ssh -L 8080:localhost:8080 username#remoteserver -N
and keep this running while you use http://localhost:8080 on your local machine to debug your remote nodejs session
Update august 2017
Start node in inspect mode:
node --inspect=0.0.0.0:9229 myproject.js
or if you want the debugger to break at the first line of myproject.js:
node --inspect-brk=0.0.0.0:9229 myproject.js
Then open the following URL in your chrome browser:
chrome://inspect
Click the 'Configure...' button and add the following target:
ip-or-name-of-server-running-node:9229
After you click the 'Done' button, you should see myproject.js under your remote targets. Click the inspect link to start debugging. Unfortunately, the inspect link does not work on Chrome 58 for Ubuntu. It works fine on Chrome 60 for Windows.
Use node-inspector to remotely debug your node application from Chrome that you've started with the --debug option as you've shown.
Recent versions of Node (> v6.3.0) and Chrome now allow you to use the Chrome Developer Tools to debug a Node.JS process without having to install anything else. Just pass --inspect to node:
$ node --inspect script.js
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://SOME-URL-HERE
Just open that URL in Chrome, and you're good to go.
If you need to pause your script immediately after Node starts, you can also pass --debug-brk in the same command.
using
$ vagrant ssh -- -L 5858:127.0.0.1:5858
to ssh connect to VM. also this comment would start a proxy server on port 5858;
you could test using telnet 127.0.0.1 5858 to see if local proxy server started or not.
In VM, you can start node with command
$ node --debug-brk app.js
set up debug configuration in web storm.
when you start debug in web storm, node.js server in VM will start in a few seconds.
PS: there is no need to touch the vagrant file.
Reference: Connecting WebStorm to a remote node.js debugging session.

Resources