node inspect
- Node.js course
- Intermediate
- Lesson 88/116
- 5 min
Learn to debug with node inspect.
What is node inspect?
Node.js's built-in debugger runs in the terminal with node inspect. The command node inspect file.js runs the code line by line, pausing at each step. It stops on the first line and you advance with commands like repl, next, cont.
Commands: cont (continue) goes to the next breakpoint; next jumps to the next line; step steps into a function call; repl opens an interactive shell so you can inspect variables. To set a breakpoint you add a debugger; line in the code; the debugger stops there. This is more powerful than console.log because you can see every variable's value at that moment.
REPL commands
When the debugger pauses, type repl to enter an interactive shell. There you can read variables and write expressions. Press Ctrl+C to leave the shell and return to the debugger.
Common commands: cont (c) runs to the next breakpoint; next (n) goes to the next line; step (s) steps into a function; out (o) steps out; setBreakpoint() sets a breakpoint; clearBreakpoint() removes one. Shortcuts are in parentheses. Type .exit to leave the debugger fully. Commands work in lowercase too; they are not case-sensitive.
Practice Write a debuggable script
Define function add(a, b) { return a + b; }. Add a debugger; line. Call with const result = add(2, 3);. Print with console.log("Result: " + result).
Starter code
// Define the add function
// write here
// Add a breakpoint
// write here
// Call add
// write here
// Print "Result: ..."
// write hereSample solution
function add(a, b) {
debugger;
return a + b;
}
const result = add(2, 3);
console.log("Result: " + result);Expected output
Sonuç: 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does node inspect file.js do?
node inspect dosya.js- Checks the syntax without running the code
- Compiles the code to machine code
- Opens the code in the browser
- Debugs the code by pausing line by line Correct answer
Explanation: node inspect debugs code by pausing.
2 Complete the inspect command
node ____ dosya.js
node inspect dosya.js
Explanation: node inspect starts the debugger.
3 Match each command to its function
| Term | Meaning |
|---|---|
cont |
Goes to next breakpoint |
next |
Jumps to next line |
step |
Steps into function |
repl |
Opens interactive shell |