Skip to content

Commit e2e67ea

Browse files
authored
docs: document opening the inspector at runtime with node:inspector (#3375)
1 parent e7bd3d5 commit e2e67ea

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

runtime/fundamentals/debugging.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
last_modified: 2026-05-20
2+
last_modified: 2026-06-30
33
title: "Debugging"
44
description: "Debug Deno programs with the V8 inspector: Chrome DevTools, VS Code and JetBrains setup, network inspection, worker debugging, and the --inspect flag family."
55
oldUrl:
@@ -93,6 +93,57 @@ this flag by default.
9393
deno run --inspect-brk your_script.ts
9494
```
9595

96+
## Activating the inspector at runtime
97+
98+
The `--inspect` flags start the inspector server when the process launches. If
99+
you instead want to open it on demand from inside a running program, use the
100+
[`node:inspector`](https://nodejs.org/api/inspector.html) module. This is handy
101+
for long-running processes such as servers, where you only want a debugger
102+
listening after some condition is met rather than for the whole lifetime of the
103+
process.
104+
105+
`inspector.open([port][, host][, wait])` starts the inspector server. The port
106+
defaults to `9229` and the host to `127.0.0.1`. Because it binds a network
107+
socket, the program needs the `--allow-net` permission (or run with `-A`).
108+
109+
```ts title="server.ts"
110+
import inspector from "node:inspector";
111+
112+
Deno.serve((req) => {
113+
if (new URL(req.url).pathname === "/debug" && !inspector.url()) {
114+
inspector.open(9229, "127.0.0.1");
115+
console.log("Inspector listening on", inspector.url());
116+
}
117+
return new Response("hello");
118+
});
119+
```
120+
121+
```sh
122+
deno run --allow-net server.ts
123+
```
124+
125+
Send a request to `/debug` and the inspector server starts; open
126+
`chrome://inspect` in a Chromium derived browser to connect. The module exposes
127+
a few related functions:
128+
129+
- `inspector.open(port, host, true)` — passing `true` as the third argument
130+
blocks until a client connects, the same as `inspector.waitForDebugger()`.
131+
- `inspector.url()` — returns the inspector WebSocket URL, or `undefined` when
132+
the inspector is not active.
133+
- `inspector.close()` — stops the inspector server.
134+
135+
A `Session` is also available for issuing
136+
[Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/)
137+
commands programmatically without an external client.
138+
139+
:::caution
140+
141+
Binding the inspector to a public IP with an open port is insecure: it lets any
142+
host that can reach the port connect to the inspector and execute arbitrary
143+
code. Keep the host at `127.0.0.1` unless you fully control the network.
144+
145+
:::
146+
96147
## Example with Chrome DevTools
97148

98149
Let's try debugging a program using Chrome Devtools. For this, we'll use

0 commit comments

Comments
 (0)