How do I debug a Node.js server application?
Right now I'm mostly using alert debugging with print statements like this:
sys.puts(sys.inspect(someVariable));
There must be a better way to debug. I know that Google Chrome has a command-line debugger. Is this debugger available for Node.js as well?
Just for completeness:
The PyCharm 3.0 + Node.js Plugin offers an awesome development + run + debug experience.
IntelliJ works wonderfully for Node.js.
In addition, IntelliJ supports 'Code Assistance' well.
A quick-and-dirty way to debug small Node.js scripts with your favorite browser debugger would be to use browserify. Note that this approach doesn't work with any applications which require native I/O libraries, but it is good enough for most small scripts.
Now move all your
var x = requires('x')
calls into arequires.js
file and run:(The downside here is that you either have to move or comment the
requires
in all your files.)Include the
bundle.js
in an HTML file like so:Now load the file in your browser and press F12 and viola: debug in browser.
Assuming you have node-inspector installed on your computer (if not, just type 'npm install -g node-inspector') you just have to run:
And paste the URI from the command line into a WebKit (Chrome / Safari) browser.
Debugging
Profiling
node --prof ./app.js
node --prof-process ./the-generated-log-file
Heapdumps
Flamegraphs
Tracing
Logging
Libraries that output debugging information
Libraries that enhance stack trace information
Benchmarking
ab -n 100000 -c 1 http://127.0.0.1:9778/
Other
Legacy
These use to work but are no longer maintained or no longer applicable to modern node versions.
If you need a powerful logging library for Node.js, Tracer https://github.com/baryon/tracer is a better choice.
It outputs log messages with a timestamp, file name, method name, line number, path or call stack, support color console, and support database, file, stream transport easily. I am the author.