Supercharge your debugging experience for Node.js

When console logs just aren’t enough

Debugging with Node

What do you do when you see this?

You might check every line of code and try to find a typo. If you find no typos, you might start using console.logs to see the output for code, which is a pain. Surely there are better methods out there?

Debugging server-side javascript code with console logs is archaic.

Console logs simply aren’t enough. They require us to slowly go line by line, restarting our server each time. Console logs also clutter our codebase with unnecessary code and removing them requires effort.

Exhibit A: how much energy does it take to spot the console log among the dummy data and other processes?

uh-oh, console cluttered. Where’s my console log?

Very daunting. It doesn’t have to be this way.

A much more efficient way of doing things would be debugging with breakpoints. With breakpoints, we can step over the call stack and event loop to diagnose the problem.

Philip Roberts: What the heck is the event loop anyway? | JSConf EU

You get the point. Time to explore other options. What are the options? I’m glad you asked!

By the way, there’s a very tasty treat at the end of the article —( just saying!).

Node has a built-in inspector!

That’s right: Node actually has a built-in inspector. The inspector uses the v8 debugger protocol viewer under the hood.

Let me show you. Let’s start a new node project:

Starting our project and installing express and nodemon:

Now we have a basic express server. But instead of using the node server.js command the traditional way, we’re adding an extra flag to our node command, the inspector.

--inspect tells Node to expose the new debugging protocol.

Once we start our server with the inspector, here’s what the console outputs:

Sweet, it worked! Open the Chrome at http://localhost:3000 with the dev tools. Notice something extra?

Notice the Node element next to the mobile inspector

Node inside our browser? Yes indeed! We still consume our app like before — localhost:3000

http://0.0.0.0:9229/ port is for the DevTools to consume.

Inspecting server side code inside our browser

What does Node Inspector do?

Node Inspector lets you use the DevTools user interface with the native Node debugger. DevTools can now connect directly to the Node process!