Introduction to the

For developers, in the process of application development, often in order to facilitate development and solve bugs need to rely on the debugging function of programming language. This is typically done with the help of the debugging capabilities of powerful ides. Nodejs is no exception.

Today we will take a closer look at how to debug nodeJS programs.

Example Enable nodeJS debugging

Remember the KOA program we talked about earlier? This article will use a simple KOA server application as an example to debug nodeJS.

Let’s start with a simple KOA service app.js:

const Koa = require('koa');
const app = module.exports = new Koa();

app.use(async function(ctx) {
  ctx.body = 'Hello World';
});

if (!module.parent) app.listen(3000);

Copy the code

The above program opens port 3000 and establishes an HTTP service. Every time you request it, it returns Hello World, very simple.

To run the above program, we need to execute Node app.js. This executes app.js but does not enable debugging.

How do you debug it?

We need to add –inspect:

node --inspect app.js
Copy the code

The above code will enable nodeJS debugging.

Let’s look at the output:

Debugger listening on ws:/ / 127.0.0.1:9229/88 c23ae3 CD - 9081-41-98 b0 - d0f7ebceab5a
For help, see: https://nodejs.org/en/docs/inspector
Copy the code

The result tells us two things. The first thing is the port on which the Debugger listens. Port 9229 127.0.0.1 will be enabled by default. A unique UUID is assigned for differentiation.

The second thing is to tell us that nodeJS uses Inspector.

Inspector was introduced after NodeJS 8, or legacy Debugger if it was before NodeJS 7.

Debugging security

If the debugger is connected to the NodeJS runtime environment, a malicious attacker can run arbitrary code in the nodeJS runtime environment, if any. It’s a huge security risk for our program.

So we must pay attention to the security of debugging. In general, remote debugging is not recommended.

By default –inspect binds to 127.0.0.1, which allows access only to native programs. And any program running locally has permission to debug the program.

If we really want to expose the debug program to an external program, we can specify the local external IP address or 0.0.0.0 (for any address, unlimited) so that the remote machine can be debugged remotely.

What if we want to do secure remote debug?

First, we need to enable local debug:

node --inspect app.js
Copy the code

Then we can set up an SSH tunnel to map the local port 9221 to the remote server port 9229:

ssh -L 9221:localhost:9229 [email protected]
Copy the code

This allows us to debug remotely by connecting to port 9221 locally.

Nodejs debugging using WebStorm

JetBrains WebStorm is a great tool for nodeJS development. WebStorm comes with debug option, which if enabled, will be enabled in the background –inspect:

Debugging using WebStorm is similar to debugging Java programs using IDEA, which is not covered here.

Use Chrome devTools for debugging

To use Chrome devTools for debugging, we have enabled –inspect mode.

Type chrome://inspect:

You can see the Chrome Inspect interface directly in Remote Target if you already have the Inspect NodeJS application enabled locally.

Select the target you want to debug and click inspect to launch the Chrome devTools debugging tool:

You can profile your application and you can debug it.

Here we are focusing on debugging, so go to the Source column and add the source code for the program you want to debug:

Add a breakpoint and start debugging. This is the same as debugging web js in Chrome.

Use Node-inspect for debugging

Nodejs has a built-in debugging tool called Node-inspect, which is a CLI debugging tool. Let’s see how it works.

We directly use:

node inspect app.js

< Debugger listening on ws:/ / 127.0.0.1:9229 / f1c64736-47 a1-42 c9-9 e9e - f2665073d3eb
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in app.js:1
> 1 const Koa = require('koa');
  2 const app = module.exports = new Koa();
  3 
debug> 

Copy the code

Node Inspect does two things. The first thing it does is generate a subroutine to run Node –inspect app.js, and the second thing it does is run the CLI debug window in the main program.

The CLI debugger provides us with some very useful commands:

  1. Stepping
  • Cont, c: Execution continues
  • Next, n: Step to next
  • step, s: Step in
  • out, o: Step out
  • Pause: pauses the code
  1. Breakpoints
  • SetBreakpoint (), sb(): Sets a breakpoint on the current line
  • SetBreakpoint (line), sb(line): Sets a breakpoint on the specified line
  • setBreakpoint(‘fn()’), sb(…) : sets a breakpoint in the specified function
  • setBreakpoint(‘script.js’, 1), sb(…) : Sets a breakpoint in the specified script file
  • clearBreakpoint(‘script.js’, 1), cb(…) : Clears breakpoints from files
  1. Information
  • Backtrace, bt: Prints the backtrace information of the current Execution frame
  • List (5): Lists the five lines before and after the source code
  • Watch (expr): Adds listening expressions
  • Unwatch (expr): Deletes a listening expression
  • Watchers: List all Watchers
  • Repl: Opens the REPL expression
  • Exec expr: Executes the expression

With the above commands, you can perform complex debugging activities in the CLI.

Other debug clients

In addition to the few mentioned above, we can also use VScode, Visual Studio, Eclipse IDE, etc., to debug nodeJS, we won’t go into details here.

Interested friends can explore on their own.

Author: Flydean program stuff

Link to this article: www.flydean.com/nodejs-debu…

Source: Flydean’s blog

Welcome to pay attention to my public number: “procedures those things” the most popular interpretation, the most profound dry goods, the most concise tutorial, many you do not know the small skills you find!