Navigation Menu

Skip to content

Debugging NodeJS Apps

shimondoodkin edited this page Dec 30, 2010 · 33 revisions

1) See what is there:
To print object use:

console.log(require('sys').inspect(some_variable))

2) Debug as usual (narrow on a bug) by using print outs

console.log("A1")
#$%^bug
console.log("A2")

3) Debug as usual (narrow on a bug) by removing buggy code (if code brakes your application)
do control+x to cut buggy code or replacing it with dummy to help figure out what code is buggy.
Then re-run the program. Do control+z to undo (or control +shift+z to redo if you undo too much)
and search for the bug again in the narrow area.
Be careful if you not use to use this risky(from the undo viewpoint) method.

4) Debug as usual by using stack trace:
In this method you print stack trace to know the call order.
You do it by making an intended error and catching it

global.tracelog=function(s){ try { throw new Error('Trace'+(s?':"'+s+'"':'')); } catch(e) { console.log(e.stack); } }; // displays a trace in the log
function a_function_that_called_accidently()
{
 tracelog('inside a function');
}
a_function_that_called_accidently()
a_function_that_called_accidently()
  • if it is the same line in the stack trace it could be a loop

5) Sometime you might need more then last 10 stack traces. so you could set the Error.stackTraceLimit.

Error.stackTraceLimit = 100 // set stack limit to 100
function x (i) { if(i < 100) return x(i + 1); throw new Error(“I=” + i) } // Make100 errors to see it works.
x(0);

Before node 0.2.2: To use debuggers you have to build node with —debug eg:

tar -vxf node.0.0.0.010000.tar.gz
./configure --debug
make 
make install

then you run your app like:
node —debug myserver.js
or like
node —debug=5858 myserver.js
the default port is 5858

6) Use ndb
ndb – works fine but with a minus that it is a command line program..

http://github.com/smtlaissezfaire/ndb

7) Use node-inspector
node-inspector looks nice and usually works very well,
http://github.com/dannycoates/node-inspector

node-inspector and cloud9ide afidk require node.js 3.0

8) use v8 debugger for eclipse << broken in the last version of node.
you have to install eclipse ,eclipse-pde ,eclipse-plugin-cvs, eclipse-jdt
then install as in google code descritopn.
http://wiki.github.com/ry/node/using-eclipse-as-node-applications-debugger