How to PROPERLY debug node.js with node inspector? How to PROPERLY debug node.js with node inspector? node.js node.js

How to PROPERLY debug node.js with node inspector?


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.


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


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