41

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

5
  • 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;. Commented Jan 17, 2012 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 Commented Jan 18, 2012 at 9:47
  • @Totty: did you find any debugger for sublime ? Commented Mar 26, 2014 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 Commented Mar 26, 2014 at 22:08
  • Possible duplicate of How do I debug Node.js applications? Commented May 31, 2016 at 15:18

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I managed to make the breakpoints work using exactly this steps.
I use $ node --inspect --debug-brk myfile.js
+1 for @troyd's answer; the --inspect --debug-brk flag combo oughtta print out a link that you cna paste in to your browser to view the devtools
Deprecated for v8.9.0.
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).

4 Comments

handy link to the internal debugger - i was wondering how i was going to do this.
Actually, I used to place debugger; statements and v8 did break on them as expected.
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.
@RyanOlds You're right, it was apparently past the debugger; statement before the debugger connected. I'll update my answer.
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

Comments

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.

1 Comment

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

Comments

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

Comments

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

1 Comment

Also put this in article format, although the information above tells it pretty well! medium.com/@theroccob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.