How to debug Node.js based web application - node.js

I have been seeking solutions for debugging Node.js based web application from days. I had tried to debug with Eclipse + Chrome debugger plugin but failed, I posted the error in this session 'Failed to get tabs for debugging' when trying to debug NodeJs with chrome debugger for Eclipse, but could not find an answer.
However, as Node.js is so popular, I have no doubt that a lot developers have the solution for debugging Node.js server JavaScript code. I appretiate if you could share me your solution of setting up an IDE (edit and debug) for Node.js server JavaScript code, or at least how to debug it.

I would suggest using node-inspector. Here is a good tutorial for setting it up. It allows you to use a browser tab to look at your code, set breakpoints, and step through it. Additionally, you can start node with the flag --debug-brk and it will insert a break point on the first line, if you need to debug something that happens on startup.

There are several tools to debug a node application. A great ressource has been postet on Github recently.
A simple debugging setup might be just adding a breakpoint to your code and start node in debug mode like this:
Example code with use of debugger, a manual breakpoint.
app.get('/foo', function(req, res) {
var something = 123;
debugger; // execution stops if this point is reached
foo(something);
});
Start node in debug mode:
$ node debug app.js
Once you started the application in debug mode you use the application (e.g., navigate to the ressource with your browser) and once a breakpoint occurs, the execution stops and you have a debugger environment in your terminal.

Try to use Weinre (WEb INspector REmote). It's a node app help you debug UI, log for web app on devices.
It hope it can help you.

Related

Debugging Selenium JS application on NodeJS

Im NodeJS newbie. I have JS application for running selenium-based tests of web aplication. My problem is that I cannot debug this application.
How to debug my JS application. My editor is Visual Studio :) or any simple text editor.
My application is started by code
node src/test/UI/app/Tests.js
Any module needed for debuging JS apps started under NodeJS?
As an example, you can debug your Node.js in WebStorm.
There are two scenarios of how you can do that.
Remote debugging. In that case you need to run you test with --inspect flag (node --inspect <pathToTest>). Then you need to create a debug configuration (port 9229 is a default debugging port used by Node.js). See details on the screenshot bellow.
Start Node.js app through WebStorm debug configuration. In that case you just have to specify another debug configuration by setting path to node binary and specifying application entry point (in that case it is your test file). Screenshot bellow illustrates details.
After that set a break point and launch your selenium test.
Happy debugging.

How do I debug Node apps that end very quickly?

I have a Node app that basically does some work and exits. This happens really fast, maybe in a second, so when I do
node --inspect app.js
I don't have enough time to open Chrome and set the breakpoint in order to stop the script.
Is there some other way to debug the script, e.g. somehow pre-set the breakpoint or make it stop immediately on the first line?
Have you had a look at the NodeJs debugger?
Node Debug
To use it, start Node.js with the inspect argument followed by the path to the script to debug.
eg.
$ node inspect myscript.js
Have a look at the breakpoint section in particular:-
Node Debugger Breakpoints
You can include:-
setBreakpoint(line)
to set breakpoints on specific lines.
You can find all the popular node.js debugging apps available in the link below :
https://nodejs.org/en/docs/inspector/
Using node-inspect, you can set a breakpoint on current line or specific line using :
setBreakPoint()
There are many other options of setBreakpoint() depending on your requirement which you can find in the documentation Here
Alternatively, I'd suggest using VSCode which has inbuilt debugger with which you can place a breakpoint in the editor itself. You can debug your node.js app in your IDE instead of opening a chrome web inspector or putting debug logs in the code and it is very simple to configure the app. Just create a launch configuration based on how you launch your node.js app and run it.
Node.js debugging with VSCode is clearly explained in their docs Here
You can do the same with WebStorm too but you need a paid license to use the WebStorm compared to VSCode which OpenSource.
Try to use the following
node --inspect-brk app.js
it will start the process with an active breakpoint at the very beginning of the program.

Debug node.js web application that has been launched via a Gulp task

I have a web application that is written in node.js and gets started using a gulp command. When the application first starts, before the server is running, debug points may be hit in WebStorm (or in any IDE or command line tool). However, after the server is running and I go to the interface in my localhost I can no longer hit debug points inside the application. This is not being caused by client side code as the debug points are in server code.
I have read the answers that involve using the node-inspector and that has not solved my problem because of configuration files that are not getting read when starting the debugger in node inspector.
I'm a bit surprised that there is so little on here about this issue. Is it not a normal problem that other developers face? Thanks in advance for the help.
My workaround for this may not be sufficient for every case. I was modifying JavaScript files and didn't actually need the configuration files, after loading, to be able to test my changes. I did the following:
Wrote a line in my app.js file that set the variables I needed. (these would have been tasks ran in Gulp)
I then started the app as a Node.js app and was able to debug it as normal.
If any of my views had been updated, or anything else that was being managed by Gulp, then I could have simply stopped the server, started it again via the Gulp command, and then stopped again and restarted as a Node.js app.
This did not solve the issue in my OP but it was a good enough workaround to get debug points in the JavaScript.

Break Node.js into debugger

Is this possible to break already running Node.js process that was started normally without any extra flags related to debugging into the debugger mode to inspect some variables for example?
I don't think that you could do something like that in a running server but you always have access to the console on chrome if you want to debug what you render.
You can try to Console.log(varNameHere) the variables you want to see.

Debugging meteor app - setup node inspector

I wanted to debug meteor code and came across couple of links:
How to debug server side code in a Meteor app
https://github.com/oortcloud/unofficial-meteor-faq#how-do-i-debug-my-meteor-app
While these two links talks about using node inspector to debug the code, it does not tells you how to run the actual application and setup the break point.
I am able to run\setup node inspector but I am not sure after that how can I launch the application and set breakpoints. Please help

Resources