# Debugging

# Debugging Client Code with Chrome

# Debugging NodeJS with Chrome

Insert at least one debugger statement where do you want set the initial stop in your code. For instance:

function* fails(arg) {
    try {
        console.log("----Error handling example---");

        debugger;
        ...
    } catch (err) { ... }
}
1
2
3
4
5
6
7
8

In the terminal:

➜   node --version
v14.4.0
➜  building-async-await-solution git:(trycatch) node --inspect-brk solution.mjs 
Debugger listening on ws://127.0.0.1:9229/cae3929d-80b4-4179-91cc-3e9ccdb1e4b7
For help, see: https://nodejs.org/en/docs/inspector
1
2
3
4
5

the option --inspect-brk=[host:port] does the following:

  • Enable inspector agent
  • Bind to address or hostname host (default: 127.0.0.1)
  • Listen on port port (default: 9229)
  • Break before user code starts

In the browser open the URL chrome://inspect and click in the inspect link:

The Chrome debugger will stop in the first debugger statement:

Insert debugger statements wherever you want to set a break-point:

# Debugging Node.js with Visual Studio

# Attach to Node.js Process

You can run the program as we explained in the chrome debugger section:

➜  building-async-await-solution git:(trycatch) ✗ node --inspect-brk solution.mjs
Debugger listening on ws://127.0.0.1:9229/64e8d287-ee15-46f4-908f-4cffbc08563d
For help, see: https://nodejs.org/en/docs/inspector
1
2
3

And then in the command palette (F1) select Debug: Attach to Node Process:

Then pick the process you want to debug:

The debugger will start controlling the process:

# Auto Attach

If the Auto Attach feature is enabled, the Node debugger automatically attaches to certain Node.js processes that have been launched from VS Code's Integrated Terminal. To enable the feature, either use the Toggle Auto Attach command from the command palette (F1)

or, if it's already activated, use the Auto Attach Status bar item

After enabling Auto Attach, you'll need to restart your terminal.

See Node.js debugging in VS Code (opens new window)

# Debug Node apps con Visual Studio Code

Includes both Javascript y Typescript. Por Albert Hernández

# References

Last Updated: 2 months ago