Debugger in Node.js: Command-Line Debugging Utility

Node.js includes a simple command-line debugger that allows stepping through code, inspecting variables, setting breakpoints, and evaluating expressions. While it is not a full-featured debugger, it provides essential functionality for quick debugging. For advanced usage, Node.js integrates with the V8 Inspector, enabling Chrome DevTools for profiling and step-by-step debugging.

node inspectBreakpointsWatchersreplV8 Inspector

~2 min read · Updated Dec 27, 2025

1. Starting the Debugger


Run Node.js with the inspect argument to start debugging:


$ node inspect myscript.js

By default, the debugger breaks on the first executable line.


2. Resume Until First Breakpoint


To run until the first debugger statement, set the environment variable:


$ NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js

3. REPL and Expression Evaluation


The repl command allows evaluating code in the current context:


debug> repl
> x
5
> 2 + 2
4

4. Watchers


Watchers track variable values at breakpoints:


  • watch('expr'): Add an expression to the watch list.
  • unwatch('expr'): Remove a watcher.
  • watchers: List all active watchers.

5. Core Debugger Commands


  • cont: Continue execution.
  • next: Step to the next line.
  • step: Step into a function.
  • out: Step out of a function.
  • pause: Pause execution.

6. Breakpoints


  • setBreakpoint(line): Set a breakpoint on a specific line.
  • setBreakpoint('script.js', 1): Set a breakpoint in another file.
  • setBreakpoint('script.js', 1, 'num < 4'): Conditional breakpoint.
  • clearBreakpoint('script.js', 1): Clear a breakpoint.

7. Information and Profiling


  • backtrace: Print the current execution stack.
  • list(5): Show 5 lines of context around the current line.
  • profile / profileEnd: Start and stop CPU profiling.
  • takeHeapSnapshot(): Save a heap snapshot.

8. Execution Control


  • run: Run the script.
  • restart: Restart the script.
  • kill: Terminate the script.

9. V8 Inspector Integration


For advanced debugging, Node.js integrates with Chrome DevTools via the V8 Inspector:


  • --inspect: Start the app and allow debugger attachment.
  • --inspect-wait: Wait for debugger before execution.
  • --inspect-brk: Break on the first line immediately.

$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/...

Conclusion


The Node.js Debugger provides essential tools for stepping through code, inspecting variables, and setting breakpoints. Combined with V8 Inspector and Chrome DevTools, developers gain powerful capabilities for profiling, memory analysis, and step-by-step debugging, ensuring more reliable and maintainable applications.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue