liveBook · Manning

Debugger statement

As explained in Essential TypeScript 5, Third Edition

A debugger statement is a JavaScript statement that requests a debugging pause when an attached debugger is active.

A debugger statement is the JavaScript keyword `debugger`, written as `debugger;` in TypeScript. When an application runs under an active debugger, execution pauses at that statement and control passes to the developer. section 6.2.1

The statement is useful when editor-created breakpoints are unreliable. Normal output may remain unchanged because Node.js ignores `debugger` by default, but the statement should be removed before deployment; linting can help catch leftovers. section 6.2.1

A Visual Studio Code debugging session can use the statement as follows:

  • Enable source maps so generated JavaScript can be correlated with TypeScript. section 6.2.1
  • Configure `.vscode/launch.json` to launch the compiled file, such as `dist/index.js`. section 6.2.2
  • Start the Node.js debugger; execution continues until it reaches `debugger;`. section 6.2.2
  • Inspect variables, set watches, step through statements, evaluate expressions, or resume execution. section 6.2.2

Pause before calculating a total

import { sum } from "./calc.js"; let message = "Hello, TypeScript"; console.log(message); debugger; let total = sum(100, 200, 300); console.log(`Total: ${total}`); section 6.2.1

Listing 6.3 Adding the debugger keyword in the index.ts file in the src folder
import { sum } from "./calc.js";

let printMessage = (msg: string): void => console.log(`Message: ${ msg }`);

let message = ("Hello, TypeScript");
printMessage(message);

debugger;

let total = sum(100, 200, 300);

console.log(`Total: ${total}`);

View the complete TypeScript listing with the debugger statement placed between the message output and total calculation. section 6.2.1

Listing 6.5 Starting the Node.js debugger
node inspect dist/index.js

Start the integrated Node.js debugger with the compiled JavaScript file. section 6.2.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains pause behavior, source-map support, debugger configuration, inspection, stepping, and deployment limitations.
Examples
Demonstrates `debugger;` in `index.ts`, Visual Studio Code launch configuration, and Node.js `inspect` commands.
What it assumes
Assumes familiarity with TypeScript compilation and JavaScript execution while teaching source maps and debugger setup.
What you can do after
Diagnose running applications by pausing execution, inspecting variables, evaluating expressions, stepping through code, and resuming.
How it connects
Connects debugger statements with source maps, breakpoints, Visual Studio Code, the Node.js debugger, and linting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.