40

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

Totty.js
  • 14,070
  • 25
  • 93
  • 165
  • 1
    Both 1 and 2 are valid complaints about node-inspector. There are other ways to debug, plugin to Eclipse or using `node --debug` plus putting break point in your code with `debugger;`. – Ryan Olds Jan 17 '12 at 16:38
  • I tried with eclipse but I given up after more than 1 week trying. Now I meet Sublime Text 2 and it's really a lot better than eclipse for what I use. I will search if exists any debugger for sublime text – Totty.js Jan 18 '12 at 09:47
  • @Totty: did you find any debugger for sublime ? – SharpCoder Mar 26 '14 at 14:56
  • No, I still use node inspector. Is very slow, so I try to use console.log when possible. Is really a pain point in node.js – Totty.js Mar 26 '14 at 22:08
  • Possible duplicate of [How do I debug Node.js applications?](http://stackoverflow.com/questions/1911015/how-do-i-debug-node-js-applications) – Ciro Santilli新疆棉花TRUMP BAN BAD May 31 '16 at 15:18

7 Answers7

26

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.

Jauco
  • 1,284
  • 12
  • 22
6

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).

mtsr
  • 2,714
  • 1
  • 12
  • 21
  • handy link to the internal debugger - i was wondering how i was going to do this. – iancrowther Apr 28 '12 at 00:29
  • 2
    Actually, I used to place debugger; statements and v8 did break on them as expected. – Paul Jun 30 '12 at 10:32
  • 1
    Co-worker just spotted my name in this answer. Is it possible that Node.js was past that `debugger;` before node-inspector is attached? I'm pretty sure V8 ignores breakpoint and `debugger;` unless a debugging client is connected. You may have better luck with --debug-brk, which tells V8 to break immediately, giving time to attach node-inspector. – Ryan Olds Apr 26 '13 at 18:50
  • @RyanOlds You're right, it was apparently past the debugger; statement before the debugger connected. I'll update my answer. – mtsr May 14 '13 at 08:42
5

The new version (0.3.x) of node inspector saves breakpoints in browser's local storage and restores them automatically.

https://github.com/node-inspector/node-inspector/pull/116

alehro
  • 2,131
  • 2
  • 24
  • 38
1

Try using IntelliJ WebStorm - there's a free trial and licenses aren't outrageously expensive. It lets you save breakpoints in all your files prior to starting up its own internal node process and remembers them across process restarts.

I agree - node-inspector looks brilliant, but is quite useless unless your app has a clear place to set a breakpoint in the top level script just after your source files have loaded, but before you hit the area you want to debug. You can structure your own code this way, but you won't be so lucky with other helpful libraries you want to include. Also... why should a debugging tool dictate your project structure!

Forgetting breakpoints is extremely unhelpful... most of my debug runs take more than one walkthrough, as in other people's code it's easy to step past where you want to be.

asparagino
  • 603
  • 6
  • 12
  • There is one advantage of node-inspector. It can navigate through Closure included files (goog.provide/goog.require), while WebStorm's debugger cannot. BTW, now node-inspector can save breakpoints, see my answer. – alehro Jul 29 '13 at 14:52
1

You can use node-codein for inspection. It won't do runtime breakpoints but it should ease the inspection process.

https://github.com/ketamynx/node-codein/

Silviu-Marian
  • 8,669
  • 5
  • 43
  • 68
0

Also worth noting.. vscode has a great debugger for node.

https://code.visualstudio.com/

Available on Mac, Linux, & Windows.

It does runtime breakpoints (without the need of writing debugger; statements), supports variable watches, and even has a call stack window (very nice).

Everything is so automated, it is now my goto over sublime text when using nodejs (and I LOVE sublime).

willko747
  • 465
  • 6
  • 12
0

This is built in now including saving breakpoints. I just tested it in node 7.3.0.

node --inspect --debug-brk app.js

This prints a url like this

To start debugging, open the following URL in Chrome: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/c3d5d93e-9d27-41b9-a4da-607e43c9d4f8

Put that in Chrome and you're good to go.

If you want to skip copy/pasting the url, do this:

npm install -g inspect-process inspect --debug-brk app.js

Unfortunately the inspect-process method doesn't retain the breakpoints :-(.

Here's a video I made: https://youtu.be/rtZKUnks6jI

RoccoB
  • 1,829
  • 1
  • 22
  • 26
  • Also put this in article format, although the information above tells it pretty well! https://medium.com/@theroccob/debug-node-js-with-chrome-devtools-aca7cf83af6b – RoccoB Apr 06 '17 at 06:20