Debugging is an integral aspect of software development, offering deep insights into application behavior and helping developers quickly identify and resolve issues. For Node.js developers, the built-in debugger accessible via node inspect provides a powerful toolset for diagnosing and resolving issues directly from the command line. This post will guide you through using node inspect and node inspect-brk, including setting up TypeScript for debugging, and effective use of debugging commands.

Setting Up Source Maps for TypeScript

To debug TypeScript effectively, you need to generate source maps, which map the transpiled JavaScript back to your original TypeScript code. Configure your tsconfig.json to enable source maps:

jsonCopy code{
"compilerOptions": {
"sourceMap": true
}
}

Transpiling TypeScript Files for Debugging

Once source maps are enabled in your tsconfig.json, transpile your .ts files using the TypeScript compiler:

tsc your-file.ts

This will generate a your-file.js file and a your-file.js.map file in the same directory.
You can now debug your-file.js using Node.js and have the debugger map the execution back to your TypeScript source code.

Getting Started with Node.js Debugger

To begin debugging, you need to start your Node.js application in debug mode. There are two main ways to do this:

  1. node inspect your-file.js: Starts your Node.js application with the debugger activated. The execution won’t pause unless it encounters a debugger; statement or an explicitly set breakpoint.
  2. node inspect-brk your-file.js: Starts your application and immediately pauses execution at the first line of the script, allowing you to set breakpoints or inspect the initial state of the application.

Using the debugger; Keyword

In your JavaScript or TypeScript code, you can insert debugger; statements at points where you want the execution to pause. This acts like a breakpoint, but it’s written directly into your code. Here’s an example:

javascriptCopy codefunction complexOperation() {
    const a = 5;
    const b = 10;
    debugger;  // Execution will pause here when debugging
    return a + b;
}

Essential Debugger Commands

  • List Code (list(n)): Displays n lines of code around the current execution point. For example, list(10) shows 5 lines before and after the current line. This is useful for gaining context about where you are in the code. Here’s how to use it effectively:bashCopy codedebugger> list(10)
  • Continue Execution (cont, c): Resumes the execution of your application until the next breakpoint or debugger statement.
  • Step Over (next, n): Moves to the next line within the same function without stepping into new functions.
  • Step Into (step, s): Steps into functions to follow the flow of execution more closely.
  • Watch Variables (watch('expression')): Adds an expression or variable to the watch list, allowing you to see its value each time the execution stops.

Advanced Techniques and Tips

  • Conditional Breakpoints: You can set breakpoints that trigger only when a specific condition is true. For example, you might only want to pause execution when a variable reaches a particular value.
  • Remote Debugging: Start Node.js with --inspect-brk=0.0.0.0:9229 for remote debugging, allowing connections from developer tools on another machine.
  • Using REPL: Type repl in the debugger to enter an interactive mode, enabling you to execute commands and access variables dynamically.

Exiting the Debugger

To exit the debugger, type .exit or press Ctrl + C twice.

Conclusion

The Node.js CLI debugger is a robust tool that can significantly enhance your debugging capabilities, particularly in server-side environments or situations where GUI-based tools are unavailable. By integrating source maps and utilizing strategic debugger; statements within TypeScript, developers can enjoy a seamless debugging experience that closely aligns with their actual codebase.

Categories:

Tags:

Comments are closed