The Talent500 Blog
How to Debug Node.js application efficiently? 1

How to Debug Node.js application efficiently?

While working on any online project, occasionally we get trapped in difficult challenges, thus we debug the backend code to grasp the scenario. Debugging is the talent that allows us to investigate these troublesome circumstances and observe exactly how our code is performed one by one. We may also investigate the variables’ values after a single instruction is performed.

Debugging is a skill that is often overlooked by developers. This ability helps you comprehend your code more correctly, and it also enhances your development skills, allowing you to write code more successfully. Placing console.log() in your code is not always a smart idea; it’s also a time-consuming chore.

Let’s start with the Prerequisites to Debug the NodeJS app in VS code

Before you debug a Node Js application, you should be aware of the following points:

1. A Basic understanding of node.js (Express)

2. Basics of JavaScript

Step-by-Step Instructions for Debugging a Node.js Application

Step 1: Create a Node.js Application

We will start with a simple Node.js application with a single endpoint and perform some random things here. Any Node.js project can be used.

For the time being, we have a very simple application that filters items from an array based on the type specified in query parameters. Below is our primary js file application. js.

How to Debug Node.js application efficiently? 2

And now for the jobs. json file containing an array of items.

How to Debug Node.js application efficiently? 3

The above express app sample is simple, with tasks being listed and filtered. The following are the endpoints for carrying out this procedure.

To fetch all tasks – http://localhost:3000/all-tasks
To filter tasks by given type – http://localhost:3000/all-tasks?taskType=personal

Let’s continue with the following stage for Nodejs debug procedure.

Step 2: Setup Node.js Debugger With “Nodemon”

Nodemon is being used to debug a Node JS application. Let’s install nodemon worldwide; to do so, use the following command:
npm install -g nodemon
or
yarn global add nodemon

Once nodemon is installed, we must use the following command to serve the node.js application with nodemon:

nodemon -L –inspect app.js.
The app.js file is the primary file of the node.js application. By running this command, your application will be delivered to a certain port, in this case 3000, therefore the application is accessible at http://localhost:3000.

Let’s go on to the next step, which is to enable the debugging option in VS code.

Step 3: Start debugger in VS code

It is now time to launch the debugger in VS code. To launch the debugger, open the command palette in your code by pressing Ctrl + Shift + p on Windows or Cmd + Shift + p in MacOS. It will take you to the next section:

How to Debug Node.js application efficiently? 4

Simply type “Debug: Attach” to get the option for “Debug: Attach Node Process.” Click on it, and you’ll be sent to the following screen:

How to Debug Node.js application efficiently? 5

These are merely node processes that are running in your system; click on the first one to view a debug controller that looks like this:

How to Debug Node.js application efficiently? 6

We’re almost done; our debugger is now turned on, and all we need to do now is set some breakpoints to stop the execution, but before we get to breakpoints, let’s first grasp some fundamental debugger and debug controller terminology.

Basic Overview of Debug Controller

We are numbering the components in debug controller, and you may follow the numbers to learn more about them. The debug controller is as follows:

How to Debug Node.js application efficiently? 7

Debug Controller

Here is the explanation of each section.

  • Play/Pause: To start and pause debugging.
  • Step In: To execute step-by-step instruction If want to execute instruction line by line then it will help you. 
  • Step into: To deep to a function or statement and debug there use this option. Also, if you are calling a function in between the script then you can simply get into those functions.
  • Restart: To Restart Debugger from the start or first breakpoint.
  • Disconnect: To stop debugging

Breakpoints

Breakpoints are an important part of debugging since they tell debuggers where to stop the execution. By adding breakpoints to your code, you are tailing to a debugger, which stops execution and tails the value of the performed variables here.

Breakpoints may be applied by clicking the left side of the editor (left side of the line number column). A screenshot of several breakpoints is shown below.

How to Debug Node.js application efficiently? 8

Now that you’ve put the breakpoint, the execution will stop here; let’s start the debugger and see how it goes.

Step 4: Run Debugger with Breakpoint

Steps 2 and 3 are required to activate the debugger. Let’s hist to the endpoint where the code will be performed after the debugger is activated.

When you reach the relevant endpoint or execution begins for the code area in which you have indicated the breakpoints, you will get the following screen:

How to Debug Node.js application efficiently? 9

This is how it looks when execution stops. Here is entire info as your code is executed line by line.

Conclusion

In conclusion, debugging Node.js applications can be a challenging task, but with the right tools and techniques, it can become a more efficient and streamlined process. By following the steps outlined in this blog, you can ensure that you are well-equipped to tackle any issues that may arise during development. Remember to start with thorough logging and error handling, utilize the built-in debugging capabilities of Node.js, and leverage third-party tools such as VS Code and NDB. With these tools and techniques at your disposal, you can debug your Node.js applications with confidence and efficiency.

 

0
Afreen Khalfe

Afreen Khalfe

A professional writer and graphic design expert. She loves writing about technology trends, web development, coding, and much more. A strong lady who loves to sit around nature and hear nature’s sound.

Add comment