Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to PROPERLY debug node.js with node inspector?

I have an app built in node.js and I use the node inspector in order to debug. But it's quite hard because of this:

  1. My breakpoints are never saved after I restart the server
  2. I cannot put a breakpoint on a file that has not loaded yet; so I have to step into from the first script to the one I want; REALLY PAINFULL!

How do you really debug node.js with node inspector?

The videos on how to use node.js are quite misleading as everything is into a module...
http://www.youtube.com/watch?v=AOnK3NVnxL8

or this one the scripts appear are already loaded in the first script
http://www.youtube.com/watch?v=HJOH0-g8f6E&feature=mfu_in_order&list=UL

Edit:

Nobody can answer this question? :s

like image 571
Totty.js Avatar asked Jan 16 '12 16:01

Totty.js


People also ask

How do I start node js in debug mode?

Open up Preferences > Settings and in the search box type in “node debug”. Under the Extensions tab there should be one extension titled “Node debug”. From here, click the first box: Debug > Node: Auto Attach and set the drop down to “on”. You're almost ready to go now.

How do you breakpoint in node JS?

When debugging in Node. js, we add a breakpoint by adding the debugger keyword directly to our code. We can then go from one breakpoint to the next by pressing c in the debugger console instead of n . At each breakpoint, we can set up watchers for expressions of interest.


2 Answers

In javascript you can set breakpoints using the debugger; statement. However, they will only pause node if a debugger is actually attached.

So launch your node script using

node --debug-brk myfile.js 

then launch node-inspector and press the play button to continue to the next breakpoint and it will hit your debugger; breakpoint (at least that works for me ATM)

(as noted in the comments: in recent versions of node you no longer have to separately install node-inspector. If you launch node using node --debug-brk --inspect myfile.js you get a url that launches the debugger in your browser).

you still need one extra click after restarting, but at least your breakpoints are saved.

if your breakpoint is not hit automatically, but only after some user action you don't need the --debug-brk of course.

like image 95
Jauco Avatar answered Oct 04 '22 15:10

Jauco


The problem with client-side breakpoints is that it's hard to keep track of the breakpoint position when the file changes. Unlike in an editor, it cannot keep track of lines being changed, etc.

@RyanOlds suggestion of using debugger; statements is also a good one, but you have to make sure the debugger is connected before the statement is evaluated, because it is ignored otherwise. Starting with --debug-brk is a good way to force this, because the execution is paused on the first line allowing you to attach the debugger and then continue the execution.

You could try debugging with node's internal debugger.

Edit: However, according to the v8 DebuggerProtocol it's possible to set breakpoints on script that hasn't been loaded yet AND you can set breakpoints by function, script and more. It should therefore be possible for node-inspector to keep track of your breakpoints (in a session, or whatever). It doesn't do so right now, though.

Maybe if v8 allows a certain piece of code to trigger a breakpoint, similar to nodes debugger? Edit: It does, you should be able to trigger a break by throwing any old exception (caught or uncaught).

like image 33
mtsr Avatar answered Oct 04 '22 15:10

mtsr