Top 10 tips for debugging Nodejs

Konfinity
February 27,2021 - 7 min read
Top 10 tips for debugging Nodejs

The entire process of developing an application, software or website is incomplete without Testing. In this blog, we are going to discuss the top 10 tips for debugging Nodejs. This blog is a compilation of some of the most unique tools you can use to debug your Node.js application.

  1. Test Driven Development

    Test-Driven Development is the process where it is required to write unit tests before the code. Unit tests rely on the repetition of short development cycles and requirements change in different test cases. In this method, the code fails at first but they pass when the developer writes clean code. In simple terms, the developer first writes a test that fails because no code has been developed yet for these tests, the developers should ensure if the test cases fail. The next step is when a programmer writes a clean code in order to pass the test cases. Now, if test cases aren’t passed, they should reiterate and write better code. The final aim is to make the code better and more efficient.

    While this is not a specific tip for Debugging Nodejs, it is important because before starting to debug a Nodejs application, one should know the basic concept of testing.

  2. The Console Log

    The console log is an important tool for testing and debugging. Almost every developer is familiar with the console log. However, most of them are unaware of various useful methods present in the console. Let’s look at some functions you can use with the console.

    • console.group

      This method creates a new collection of JavaScript console logs and it nests all the logs together in the browser. In the next point we will discuss the console.table.

    • console.table

      The console.table takes an array or an object and outputs it as an ASCII table. It is popular for long or large objects with many properties as it gives more structure that helps with readability.

    • console.dir

      Console.dir takes two arguments, the first argument takes an object and the second argument takes an object of options. In the second option, the depth of the console log is specified. The console.dir and depth property makes it easy to analyse the output.

    We looked at three important console commands and now it’s your turn to use these methods to improve your debugging experience. These methods will help you get a structured output rather than just plain console logs, this will be more suited to your debugging style.

  3. Debugger Statement

    The debugger statement is a simple way to get started with debugging. If the developer tools are open, a debugger statement pauses the execution of the code in the browser. Whereas in Nodejs, there is no developer console and hence the equivalent would have to be done by using Node.js’ inspect. Now an interactive debugger will open which will help you to step through code execution.

    This is a primitive method of debugging which might work for small scale applications in Nodejs but for advanced debugging, you need a more powerful tool. However, you should know this method of debugging also as one should know all diverse ways and how they work. In the following points, we would look at some advanced ways of debugging a Nodejs application.

  4. Inspect Flag

    Inspect flag brings back a lot of debugging capabilities in the browser. The inspect flag launches the CLI debugger. The work of the inspect flag is not too complex. First, the developer has to tell Nodejs that the application should run in debugging mode by passing a flag to the node process before the Node application is started. A WebSocket connection will be started so that you can remotely debug from the browser. If the browser you are using is Chrome, you can go to the chrome://inspect#devices page. Here you’ll see a section called ‘remote target’ which shows a list of all the running node processes that you are using and the ones that have been executed with the inspect flag. Click on the preferred application and a debugging terminal will open in Chrome.

    This is how you can execute debugging with the inspect flag. It’s a good method if you are trying to catch some small bugs. However, it’s the only one, let’s look at some other debugging method in Nodejs.

  5. IDE

    IDEs use the inspect flag to allow developers to do remote debugging without leaving their IDE. The working of IDE tools is very similar to other debugging tools. An inspector to the node process is attached and then listen on the WebSocket. The WebSocket is created for debugging purposes. We will not make use of the inspect flag as we are setting up the node process with inspect mode through the IDE. In this example, we will see how to set up debugging in VS Code.

    As part of the first step, copy the following Express application. Save this file as app.js:

    const express = require('express'); const app = express (); app.get('/', (req, res) => { res.send('Hello Konfinity!'); }); app.listen(3000, () => console.info('Debuging app listening on port 3000!'));

    The application be run by writing the command - node app.js

    The output would like - Debugging app listening on port 3000!

    After the app is running, go to VS Code and open the command palette by pressing the three keys Shift + Cmd + p simultaneously and select the option Attach to Node Process and then select the option to attach the debugger in another menu of the running node processes.

    After this step, the running node process is accessible for debugging and the console should show a message - Debugger listening on ws://__

    As the last step, open the command palette again to select the debug option and the console should display option ‘Debugger attached.’ If the IDE shows an active orange bottom bar, you are up for debugging.

  6. Postman

    Nodejs is very popular for building RESTful APIs and Postman is an important application for setting up, saving, and running API commands. With this application, different API requests can be set up and can also be fired off at the click of a button. With Postman, developers can execute pre- and post-scripting which will allow you to dynamically update requests before they are sent or save values from the response so that they can be used in subsequent requests. Postman allows you to run a list of chained commands and pass through the returned values. It is a very useful application and especially if you are making advanced RESTful APIs with Nodejs.

  7. Logging/APM tooling

    It is essential to try and debug an application locally in the same way as one would do in production. One of these tools is to log in to your remote server in order to view the console logs which is very similar to the one on local. However, it’s a tedious approach but there are always some tools that perform log aggregation. Tools such as these send the logs from a running application to a single location. With these tools, one can easily parse and visualize logs. There are some logging libraries too with the help of which one can format logs and stream them to a centralized log aggregator system. Logs can also be applied with a log level which is an environment-specific configuration. This is one useful method.

    APM that stands for Application Performance Monitoring is another tool for remote debugging in Nodejs applications. It extracts the stack traces inside the code and sends them to a central location for further analysis.

    All these tools are very useful if you are building Node.js applications. Before getting into the core development process, you should think about observing and monitoring your system right from the beginning. It’s important to learn how to visualize your Node.js application.

  8. Test performance

    Tools for test performance are integral for a developer to learn because there are some issues that crop up only during high load. One popular as well as a reliable tool for performance testing Node.js is Artillery. With this tool, one can create a YAML configuration file that will define the type of performance tests that a developer might want to run. These tools like Artillery can help you get a better picture of how your system behaves by stimulating some particular test cases according to your choice. This helps extensively with the entire process of debugging.

  9. Flame Graphs

    Flame graphs are a way to understand by visualising the execution of a function in JavaScript. The execution of a function is a little complex. In the execution of a function, it calls for another function and another and so on. At the end of this chain, one will return to the subsequent function, which will again return to the function that called it and continues until the call stack becomes free.

    It is useful for developers to understand this execution but it is not really possible with only a console.log and that is where flame graphs come in the picture.

  10. Debugging Memory Leaks

    Proper memory allocation is very important in every Nodejs application. One problem with memory allocation is memory leak, which occurs when there is improper freeing up of memory. This can lead to large amounts of memory being taken up by an application and there might arise a situation where the application runs out of memory and crashes. In JavaScript, global variables, closures, call back functions etc. can cause memory leaks.

    Hence, debugging memory leaks is just as important! You can use the inspect tag and see the memory tab which will allow you to profile your memory usage in the Node.js application and find out the memory leaks.

    These are 10 tips on how to debug your Nodejs application. Hopefully, you are inspired to debug your Nodejs application and start your journey of successful software and application development.

    Also, if you want to learn web development and start your journey as a successful web developer, we highly recommend you to take on a professional web development course.

    Out of the many available, the course that we recommend would be Konfinity’s Web Development Course The course is well-researched and is one of the most beneficial training courses out there. It is developed by experts from IIT DELHI in collaboration with tech companies like Google, Amazon and Microsoft. It is trusted by students and graduates from IIT, DTU, NIT, Amity, DU and more.

    We encourage technocrats like you to join the course to master the art of creating web applications by learning the latest technologies, right from basic HTML to advanced and dynamic websites, in just a span of a few months.

    Konfinity is a great platform for launching a lucrative tech career. We will get you started by helping you get placed in a high paying job. One amazing thing about our course is that no prior coding experience is required to take up our courses. Start your free trial here.

Chat with us