Sure it's important to debug for any programming language, and it should be learnt at first time to use a new language. I forgot why I put this debug blog at the end of the serials blogs. No matter what's the reason, now we need to introduce how to debug
nodejs. This article has two parts, one is the native debugger for nodejs, and another one is Node Inspector. In fact there is the third way to debug nodejs, based on Eclipse, which is not introduced here, but you can find more introduction from here: Using
Eclipse as Node Applications Debugger.
NodeJS Built-in debugger
-
Prepare to debug Have below simple code as our sample, name it as 'debug.js'.
var count = 10;
var sum = 0;
debugger;
for (var i = 1; i < count; i++) {
sum += i;
}
console.log(sum);
Note we have debugger;
at line 3, which means we put a breakpoint here. Actually it's the same with the normal Javascript running in browser Chrome, in which put a debugger;
statement. The difference it we add this statement into
js code running in browser to force the execution to pause at load time. But in to debug nodejs, it's not necessary, because in debug mode the execution is always paused at first line.
。。。。。。
Node Inspector
Finally we finish the basic commands of built-in debugger. Now we introduce a debug tool with UI