What is the best way to debug Node.js?

0
604

Debugging – the process of finding and fixing defects in software – can be a challenging task to do in all languages. Node.js is no exception.

How to Debug Node.js with the Best Tools Available

Luckily, the tooling for finding these issues improved a lot in the past period. Let’s take a look at what options you have to find and fix bugs in your Node.js applications!

We will dive into two different aspects of debugging Node.js applications – the first one will be logging, so you can keep an eye on production systems, and have events from there. After logging, we will take a look at how you can debug your applications in development environments.

Logging in Node.js

Logging takes place in the execution of your application to provide an audit trail that can be used to understand the activity of the system and to diagnose problems to find and fix bugs.

For logging purposes, you have lots of options when building Node.js applications. Some npm modules are shipped with built in logging that can be turned on when needed using the debug module. For your own applications, you have to pick a logger too! We will take a look at pino.

Before jumping into logging libraries, let’s take a look what requirements they have to fulfil:

  • timestamps – it is crucial to know which event happened when,
  • formatting – log lines must be easily understandable by humans, and straightforward to parse for applications,
  • log destination – it should be always the standard output/error, applications should not concern themselves with log routing,
  • log levels – log events have different severity levels, in most cases, you won’t be interested in debug or info level events.

The debug module of Node.js

Recommendation: use for modules published on npm

Let’s see how it makes your life easier! Imagine that you have a Node.js module that sends serves requests, as well as send out some.

Once you have it, start your application this way:

The output will be something like this:

Output of Node.js Debugging

Also, the debug module supports wildcards with the *character. To get the same result we got previously, we simply could start our application with DEBUG=http:* node index.js.

What’s really nice about the debug module is that a lot of modules (like Express or Koa) on npm are shipped with it – as of the time of writing this article more than 14.000 modules.

The pino logger module

Recommendation: use for your applications when performance is key

Pino is an extremely fast Node.js logger, inspired by bunyan. In many cases, pino is over 6x faster than alternatives like bunyan or winston:

Getting started with pino is straightforward:

The above snippet produces the following log lines:

The Built-in Node.js Debugger module

Node.js ships with an out-of-process debugging utility, accessible via a TCP-based protocol and built-in debugging client. You can start it using the following command:

This debugging agent is a not a fully featured debugging agent – you won’t have a fancy user interface, however, simple inspections are possible.

You can add breakpoints to your code by adding the debuggerstatement into your codebase:

This way the execution of your script will be paused at that line, then you can start using the commands exposed by the debugging agent:

  • cont or c – continue execution,
  • next or n – step next,
  • step or s – step in,
  • out or o – step out,
  • repl – to evaluate script’s context.

V8 Inspector Integration for Node.js

The V8 inspector integration allows attaching Chrome DevTools to Node.js instances for debugging by using the Chrome Debugging Protocol.

V8 Inspector can be enabled by passing the --inspect flag when starting a Node.js application:

In most cases, it makes sense to stop the execution of the application at the very first line of your codebase and continue the execution from that. This way you won’t miss any command execution.

I recommend watching this video in full-screen mode to get every detail!

How to Debug Node.js with Visual Studio Code

Most modern IDEs have some support for debugging applications – so does VS Code. It has built-in debugging support for Node.js.

What you can see below, is the debugging interface of VS Code – with the context variables, watched expressions, call stack and breakpoints.

VS Code Debugging Layout                                    Image credit: Visual Studio Code

One of the most valuable features of the integrated Visual Studio Code debugger is the ability to add conditional breakpoints. With conditional breakpoints, the breakpoint will be hit whenever the expression evaluates to true.

If you need more advanced settings for VS Code, it comes with a configuration file, .vscode/launch.json which describes how the debugger should be launched. The default launch.json looks something like this:

That’s it for now, if you have any queries then feel free to comment down below

Suggest

Angular 2 (or 4) & NodeJS – The Practical MEAN Stack Guide

RESTful Web API Design with Node.js – Second Edition

The Complete Node.js Developer Course (2nd Edition) 

LEAVE A REPLY

Please enter your comment!
Please enter your name here