How do I debug Node apps that end very quickly? - node.js

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.

Related

How to set up one-click debugging for an application with multiple processes in WebStorm?

I have an application that requires a bunch of moving parts to run, and I'd like to set things up so WebStorm can launch all of it.
An example of what I need to do:
Open a SSH tunnel to the database, which can be accomplished using a script.
Start the API using a NPM script that runs nodemon
Attach a debugger set to automatically reconnect to nodemon
Run the angular development server for the front-end
Run a HTTP reverse proxy to put all of this into the same URL space - the frontend calls the API using root-relative URLs, and the backend doesn't allow CORS
Debug the app in Chrome
This presented me with a bunch of problems:
I can't express the dependencies with "Before launch" that simply won't fire up, because WebStorm waits for the "before" tasks to finish; obviously these process actually need to run continuously.
Compound configurations can only run all of their component configurations in "Run" mode or in "Debug" mode. You can also only use the mode that all of the components have; so e.g. if you have a script configuration (which cannot be debugged) and an attached debugger (which cannot be run without debugging) you'll end up with a configuration that can't be run at all.
If I use a compound configuration without the tunnel that still means the backend will be run in debug mode which I actually don't want, because I'm attaching to it from another configuration. The frontend server will also be run in debug mode which is also pointless.
nodemon in general seems kind of flaky when run from WebStorm, possibly due to NPM; when I stop the debug it it actually doesn't do anything, and when I kill the process afterwards nodemon says it's "restarting child process" but it does actually end nodemon; except WebStorm doesn't notice this, you have to close the debugger tab yourself.
Has anybody managed to get something like this running without actually having to fire up every step on its own?
No way currently... Using Multirun plugin gives you more flexibility (see https://github.com/rkhmelyuk/multirun/wiki/How-to-run-configurations-with-Multirun), but still you can either run or debug all configurations, there is no way to start one configuration in debugger and run the others.
We have a feature request for such option, please follow IDEA-156398 for updates

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.

Having nodemon and debugging both working in a WebStorm run configuration

This is a problem I have never really been able to sort and have come across a few times so I thought I'd ask on here to see if there is a solution.
I am currently building a NodeJS(koa) application using babel to transpile the ES6 code. I have setup a run configuration which looks like this:
This setups a debug configuration in order for me to debug my ES6 code.
This works great but I want the debugger to be able to run using nodemon so that when i make changes to the code, the run configuration restarts the server but keeps the debugging functionality. Is this possible from a run configuration or does it have to be done from the command line? At the moment I can only run the debugger OR nodemon... not both at the same time.
Thanks!

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.

How to debug Node.js based web application

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.

Resources