Chapter 18

Debugging Modules and Applications

WHAT’S IN THIS CHAPTER?

  • Formatting and outputting variables
  • Using variable interpolation
  • Using the Node debugger
  • Using a visual Node debugger

Building functional software is not a trivial task. Dividing your application code into small modules and testing each one thoroughly can help, but you’re still bound to find problems. If you find yourself in a situation where you need to inspect the inner workings of your Node application or module code, several tools can help. This chapter covers several of those debugging tools – console.log, Node’s built-in debugger, and Node Inspector.

USING CONSOLE.LOG

Node has some global objects that you can use without requiring them explicitly. One of them is the console object, which enables you to output formatted strings.

The simplest debugging tool at your disposal is console.log. This function call does two things – it serializes and concatenates your objects into a string and outputs the result to the standard output stream. You can use it to inspect objects like this:

var obj = {a: 1, b: 2};
console.log(obj);

This last snippet prints the following:

{ a: 1, b: 2 }

This simple example passes in an object that is translated into a literal string representation. You can pass in any type of object, and console.log will inspect its properties and values.

NOTE Actually, console.log does not do any formatting. All the formatting is done by the util.format function. Whatever arguments you pass in to ...

Get Professional Node.js: Building Javascript Based Scalable Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.