Debugging Node.js with Google Chrome

Debugging Node.js with Google Chrome

In?software development, debugging is used when a developer locates a?code?error in a particular program and is able to reproduce it. This post describes how to debug node applications using the latest Google Chrome DevTools.

Inserting?console.log()?to code is possibly one of the most common practices for developers. However, we stop to use the console.log() in our code. We can debug into each line of the function instead of specific points with the help of Chrome DevTools. It provides us few important functionalities that?console.log()?isn’t able to provide.?

In earlier versions of Chrome, we need to manually enable the debugging features of node but with Chrome 54+ it enables as the default?

First, we need to install the current version of the node and write a program to debug it in Chrome DevTools. We can use our favorite editor to write the code, I am using Visual Studio Code.

Here I create one file called app.js under the node_debug_example folder:

let items = [111, 124, 258, 244, 207]

let totalItems = 0;

for (let i = 0; i <= items.length; i++) {

? totalItems += orders[i];

console.log(totalItems);

};        

After that open the terminal in Visual Studio Code and run the below command :?

$ node --inspect-brk ?app.js        

In node 6, we have to use --inspect --debug-brk for this inspect.

When –inspect is stared, a node.js process listens for a debugging client. It will listen at host and port would be like?127.0.0.1:9229.

Node services also start listing debugging messages if they have a SIGUSR1 signal. In node 7 and earlier, the SIGUSR1 signal activates the legacy debugger API. In the latest node versions (node.js 8), it activates the Inspector API.

It is not safe if the debugger is bound to a public IP address, or to 0.0.0.0, anyone who can reach your IP address will be able to connect to the debugger without any restriction and will be able to run random code.

node --inspect?binds to 127.0.0.1 by default. We need to provide a public IP address or 0.0.0.0, etc. if we propose to allow external connections to the debugger. Inspector allows local applications to access it.

‘$ node --inspect-brk ?app.js’ command display the host address, port, and UDDI for connection as below :?

Windows PowerShel

Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS F:\node_debugg_example> node --inspect-brk? app.js

Debugger listening on ws://127.0.0.1:9229/140b121d-ed13-432a-9dcb-8112b8c62cf9

For help, see: https://nodejs.org/en/docs/inspectorl        

Next, we need to open chrome and paste the below URL:

chrome://inspect/#devices        

It will display the screen like:

No alt text provided for this image

Node fetches the inspect which is executed from the above command and it is displayed here, on click to inspect it allow us to debug our application.

No alt text provided for this image







要查看或添加评论,请登录

Sandip Poojara的更多文章

  • A new way to validate Angular Forms

    A new way to validate Angular Forms

    If you're working on more data-oriented advanced reactive forms, you'll find that not just inputs, but some of the…

  • Razor Pages in ASP.NET Core

    Razor Pages in ASP.NET Core

    In this blog, we are going to discuss Razor pages with Asp.net Core.

    1 条评论
  • Best Practices when using Angular CLI

    Best Practices when using Angular CLI

    Angular is one of the best platforms to create Single Page Applications (SPA). We will go through several practices…

    1 条评论
  • Angular Syncfusion grid

    Angular Syncfusion grid

    Angular is one of the exceptional frameworks of JavaScript. We can use Essential JS 2 grid with angular, which is…

  • MEAN Stack CRUD- Angular 13

    MEAN Stack CRUD- Angular 13

    In this blog, we will look into how to perform Angular 13 crud operation using MEAN stack technology. We will use…

  • Virtual Scrolling- Angular

    Virtual Scrolling- Angular

    Sometimes we as a developer have to show thousands of elements in a single table. When we add these items to one DOM…

  • Interaction of NodeJS Services with JSON content using AJAX

    Interaction of NodeJS Services with JSON content using AJAX

    Node.js is an "open-source server-side runtime platform.

  • Lazy Loading Modules in Angular 8

    Lazy Loading Modules in Angular 8

    Lazy loading is a very helpful concept in Angular. It loads NgModuls only when we navigate to rout.

    1 条评论

社区洞察

其他会员也浏览了