In software development, the process of finding and resolving bugs is called debugging. Since various defects prevent whole systems from correct operation, the role of debugging is difficult to overestimate. The complexity of this process depends on several factors, such as the size of the system, its structure, the way how it interacts with other systems, etc. Therefore, modern developers know and use a lot of debugging techniques and tools. In the following post, we discuss basic aspects related to this complicated process and introduce you to the most reliable debugging tools for Node.js. Let’s start our journey with the debugging algorithm.
The first step of debugging consists in reproducing the problem. Sometimes, it requires a lot of time and effort to achieve this goal, especially in case of parallel processes or unusual problems. Specific environments can complicate the task as well.
When you are done with the first step, it is often necessary to simplify the input of the program and make it easier to debug. Let’s look at the example with a compiler and a large source file. Imagine the situation when the compiler crashes while trying to parse the file. However, after some magic with the test case, you can discover that only few lines from the original source file are responsible for that crash. Then, you remove some parts of the original test case and check if your problem still occurs.
Now, it’s time to examine program states and find out the origin of the problem with the aid of a debugging tool. Besides, you can use tracing as an alternate solution. In simple cases, it consists of only a few print statements aimed to output the variables values at certain points of program execution.
Popular debugging techniques are:
Print debugging. This debugging technique is also called tracing. It consists in watching trace or print statements, that indicate the process execution flow.
Remote debugging. The name of this techniques speaks for itself: it requires connecting to a remote system over a network to find and fix the problem.
Post-mortem debugging. With post-mortem debugging, you can easily find and fix problems after the system has already crashed.
“Wolf fence” algorithm. This popular approach to debugging exists since 80’s and is implemented in the Git version control system as the command git bisect. It required dividing the system into 2 equal parts. After the first segmentation, you find out what part of the system contains the problem and split it once more. It is necessary to repeat the procedure until you find the bug.
Delta Debugging. This is a technique of automating test case simplification.
Saff Squeeze. This one helps to isolate failure within the test with the aid of the failing test parts progressive inlining.
Node.js Debugging Tools
Below, you will find the list of the most popular debugging tools for Node.js. Since the Firebear team is passionate about this relatively new platform, we’ve tried to gather all possible solutions that will be helpful in your Node.js development.
Debug by Visionmedia
Debug by Visionmedia is a tiny utility for Node.js debugging. The tool is aimed at both libraries and apps. Besides Node.js, Debug by Visionmedia is also suitable for working in a browser. Follow the link below for further information.
Step over, step in, step out, resume;
Break on exceptions;
Continue to location;
CPU and HEAP profiling;
The inspection of network client requests and console output.
Since this Node.js debugger relies on WebSockets, there is no polling for breaks. It also supports the aforementioned remote debugging, live edit of running code, and breakpoints for files that are not loaded into V8.
Atom Node Debugger is a simple debugging tool for both Node.js and io.js, which is very convenient for daily use. You only have to open your JS file and execute the start-resume command by clicking F5. The command will launch the Node.js debugger, and its debug panels will show up. Then, you will be able to set breakpoints, resume debugging, and perform other actions necessary to find the problem. You can find more information about Atom Node Debugger on its GitHub page.
If you prefer working within Visual Studio, you can easily turn it into Node.js IDE with the NTVS plugin. Although the solution is designed, developed, and supported by Microsoft, it is free and open source. This Node.js debugging tool offers all necessary features, such as editing, profiling, debugging locally and remotely. Besides, NTVS supports Azure Web Sites and Cloud Services; Intellisense, TypeScript, and npm.
As for Longjohn, it provides useful stack traces with configurable call trace length by collecting large amounts of data. This Node.js debugging solution was elaborated for development and testing environments. Since Longjohn puts a lot of strain on V8’s garbage collector, it slows down heavily-loaded apps, therefore it is not recommended to use it in production.
probe name and pid/process title/hostname filtering;
map/reduce style reporting with remote messaging;
multi-process support and real-time cluster inspection.
With njsTrace, you will turn Node.js tracing and instrumentation into a piece of cake. The debugger provides the ability to see all function calls, return values, arguments, the time spent for each function. njsTrace is detaily described on its GitHub page. The information listed there covers such important topicks as installation and usage, including njsTrace object and formatter.
Node vStream will be helpful when you are working with Node streams, because it provides the ability to inspect a pipeline by streaming mix-ins. Instrument one or more regular Node streams by calling vstream.wrapStream on them and get the same stream but with attached functions and private properties.
Stackman helps to debug errors by providing an array of stack frames. Each frame in the stack trace offers detailed information necessary for solving a problem. As a developer, you get access to the actual source code with surrounding lines for a place where your error occurred. Stackman shows if it happened in Node.js, inside a 3rd party module, or in your code. For further information, check out the debugger’s page on GitHub.