Break Node.js into debugger - node.js

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.

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

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.

Node.js unresponsive until key press on console

I have a Node.js app, that runs on a Windows Server 2008. I'm facing a weird problem here.
My app does more than one async tasks. Sending data to socket.io, MySQL, HTTP connections, TCP connections etc.
My Node app becomes unresponsive especially on the socket.io end, and only starts responding back again after any key press on the console. Is this a known issue? Any bypasses around such a behavior?
Note: I am using console.log heavily to keep track of the current operation and errors in process.
I did a bit of a background check and came to the following conclusions.
The Powershell console on Windows Server 2008 goes on a 'pause' mode when clicked on it. It is released again if you click elsewhere.
http://www.vistax64.com/powershell/112032-script-pauses-when-you-click-powershell-text-window.html
If you notice, console.log is async. But console.warn and console.error are blocking. These are not async.
The console pause might block some/most part of the Node.js operations. Esp. as Socket.io at default debugging level will report with more than just console.log.
Solutions that other people have suggested didn't work for me, but using Cmder to start the script solved it. Definitely appears to be a console application problem, rather than a node.js problem.
I faced the same problem in a Windows Machine with my Node.js server running on PowerShell.
I solved it disabling Quick Edit Mode and Insert options in the Properties panel of PowerShell. It seems like pause the process when you click on the terminal with those two options enabled.
Hope this helps you, bro.

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