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
