To activate debugging capabilities run Deno with the or
--inspect-brk
flags.
The --inspect
flag allows attaching the debugger at any point in time, while
--inspect-brk
will wait for the debugger to attach and will pause execution on
the first line of code.
Let’s try debugging a program using Chrome Devtools. For this, we’ll use
from
std
, a static file server.
Use the --inspect-brk
flag to break execution on the first line:
In a Chromium derived browser such as Google Chrome or Microsoft Edge, open
and click Inspect
next to target:
You might notice that DevTools pauses execution on the first line of
_constants.ts
instead of file_server.ts
. This is expected behavior caused by
the way ES modules are evaluated in JavaScript (_constants.ts
is left-most,
bottom-most dependency of file_server.ts
so it is evaluated first).
At this point all source code is available in the DevTools, so let’s open up and add a breakpoint there; go to “Sources” pane and expand the tree:
Looking closely you’ll find duplicate entries for each file; one written
regularly and one in italics. The former is compiled source file (so in the case
of .ts
files it will be emitted JavaScript source), while the latter is a
source map for the file.
Next, add a breakpoint in the listenAndServe
method:
As soon as we’ve added the breakpoint, DevTools automatically opens up the source map file, which allows us step through the actual source code that includes types.
Now that we have our breakpoints set, we can resume the execution of our script so that we can inspect an incoming request. Hit the “Resume script execution” button to do so. You might even need to hit it twice!
Once our script is running, try send a request and inspect it in Devtools:
$ curl http://0.0.0.0:4507/
At this point we can introspect the contents of the request and go step-by-step to debug the code.
Deno can be debugged using VSCode. This is best done with help from the official
vscode_deno
extension. Documentation for this can be found
here.
Any client that implements the DevTools protocol should be able to connect to a Deno process.