Node js Debugger
Core Debugger
The most basic debugging tool in Node.js is Console output. The console.log() is commonly used when debugging. It provides a quick way to see what is happening at some point in a script, it doesn't break and interrupt the flow. It is fast and informative.
Example
fs = require('fs');
fs.readdir(process.cwd(), function (err, files) {
if (err) {
console.log(err);
return;
}
console.log(files);
});
Node.js Debugger
Node.js provides a debugging utility to keep the logs. This is useful when we need to see the call stack or puts the logs. This allows breakpoint to be set in a program in places where state or other aspects of the runtime need to be checked.
It is invoked using the debug statement, put debugger statements in your code and use the following to start the debugging process.
$ node debug program.js
program.js
var http = require('http');
debugger;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
debugger;
res.end('Hello World\n');
}).listen(8080, '127.0.0.1');
console.log('Server running at http://122.3.4.1:8080/');
Debugging with Node Inspector
The built-in Node.js debugger client is not intuitive because of the lack of a GUI. Node Inspector is based on the chrome developer tools. It provides more developer interface.
To download and install Node Inspector, we use our NPM in the global mode.
$ npm install -g node-inspector