{ "type": "module", "source": "doc/api/debugger.md", "introduced_in": "v0.9.12", "stability": 2, "stabilityText": "Stable", "miscs": [ { "textRaw": "Debugger", "name": "Debugger", "introduced_in": "v0.9.12", "stability": 2, "stabilityText": "Stable", "type": "misc", "desc": "
Node.js includes a command-line debugging utility. The Node.js debugger client\nis not a full-featured debugger, but simple stepping and inspection are\npossible.
\nTo use it, start Node.js with the inspect
argument followed by the path to the\nscript to debug.
$ node inspect myscript.js\n< Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8\n< For help, see: https://nodejs.org/en/docs/inspector\n<\nconnecting to 127.0.0.1:9229 ... ok\n< Debugger attached.\n<\n ok\nBreak on start in myscript.js:2\n 1 // myscript.js\n> 2 global.x = 5;\n 3 setTimeout(() => {\n 4 debugger;\ndebug>\n
\nThe debugger automatically breaks on the first executable line. To instead\nrun until the first breakpoint (specified by a debugger
statement), set\nthe NODE_INSPECT_RESUME_ON_START
environment variable to 1
.
$ cat myscript.js\n// myscript.js\nglobal.x = 5;\nsetTimeout(() => {\n debugger;\n console.log('world');\n}, 1000);\nconsole.log('hello');\n$ NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js\n< Debugger listening on ws://127.0.0.1:9229/f1ed133e-7876-495b-83ae-c32c6fc319c2\n< For help, see: https://nodejs.org/en/docs/inspector\n<\nconnecting to 127.0.0.1:9229 ... ok\n< Debugger attached.\n<\n< hello\n<\nbreak in myscript.js:4\n 2 global.x = 5;\n 3 setTimeout(() => {\n> 4 debugger;\n 5 console.log('world');\n 6 }, 1000);\ndebug> next\nbreak in myscript.js:5\n 3 setTimeout(() => {\n 4 debugger;\n> 5 console.log('world');\n 6 }, 1000);\n 7 console.log('hello');\ndebug> repl\nPress Ctrl+C to leave debug repl\n> x\n5\n> 2 + 2\n4\ndebug> next\n< world\n<\nbreak in myscript.js:6\n 4 debugger;\n 5 console.log('world');\n> 6 }, 1000);\n 7 console.log('hello');\n 8\ndebug> .exit\n$\n
\nThe repl
command allows code to be evaluated remotely. The next
command\nsteps to the next line. Type help
to see what other commands are available.
Pressing enter
without typing a command will repeat the previous debugger\ncommand.
It is possible to watch expression and variable values while debugging. On\nevery breakpoint, each expression from the watchers list will be evaluated\nin the current context and displayed immediately before the breakpoint's\nsource code listing.
\nTo begin watching an expression, type watch('my_expression')
. The command\nwatchers
will print the active watchers. To remove a watcher, type\nunwatch('my_expression')
.
cont
, c
: Continue executionnext
, n
: Step nextstep
, s
: Step inout
, o
: Step outpause
: Pause running code (like pause button in Developer Tools)setBreakpoint()
, sb()
: Set breakpoint on current linesetBreakpoint(line)
, sb(line)
: Set breakpoint on specific linesetBreakpoint('fn()')
, sb(...)
: Set breakpoint on a first statement in\nfunction's bodysetBreakpoint('script.js', 1)
, sb(...)
: Set breakpoint on first line of\nscript.js
setBreakpoint('script.js', 1, 'num < 4')
, sb(...)
: Set conditional\nbreakpoint on first line of script.js
that only breaks when num < 4
\nevaluates to true
clearBreakpoint('script.js', 1)
, cb(...)
: Clear breakpoint in script.js
\non line 1It is also possible to set a breakpoint in a file (module) that\nis not loaded yet:
\n$ node inspect main.js\n< Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9\n< For help, see: https://nodejs.org/en/docs/inspector\n<\nconnecting to 127.0.0.1:9229 ... ok\n< Debugger attached.\n<\nBreak on start in main.js:1\n> 1 const mod = require('./mod.js');\n 2 mod.hello();\n 3 mod.hello();\ndebug> setBreakpoint('mod.js', 22)\nWarning: script 'mod.js' was not loaded yet.\ndebug> c\nbreak in mod.js:22\n 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.\n 21\n>22 exports.hello = function() {\n 23 return 'hello from module';\n 24 };\ndebug>\n
\nIt is also possible to set a conditional breakpoint that only breaks when a\ngiven expression evaluates to true
:
$ node inspect main.js\n< Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35\n< For help, see: https://nodejs.org/en/docs/inspector\n<\nconnecting to 127.0.0.1:9229 ... ok\n< Debugger attached.\nBreak on start in main.js:7\n 5 }\n 6\n> 7 addOne(10);\n 8 addOne(-1);\n 9\ndebug> setBreakpoint('main.js', 4, 'num < 0')\n 1 'use strict';\n 2\n 3 function addOne(num) {\n> 4 return num + 1;\n 5 }\n 6\n 7 addOne(10);\n 8 addOne(-1);\n 9\ndebug> cont\nbreak in main.js:4\n 2\n 3 function addOne(num) {\n> 4 return num + 1;\n 5 }\n 6\ndebug> exec('num')\n-1\ndebug>\n
",
"type": "module",
"displayName": "Breakpoints"
},
{
"textRaw": "Information",
"name": "information",
"desc": "backtrace
, bt
: Print backtrace of current execution framelist(5)
: List scripts source code with 5 line context (5 lines before and\nafter)watch(expr)
: Add expression to watch listunwatch(expr)
: Remove expression from watch listunwatch(index)
: Remove expression at specific index from watch listwatchers
: List all watchers and their values (automatically listed on each\nbreakpoint)repl
: Open debugger's repl for evaluation in debugging script's contextexec expr
, p expr
: Execute an expression in debugging script's context and\nprint its valueprofile
: Start CPU profiling sessionprofileEnd
: Stop current CPU profiling sessionprofiles
: List all completed CPU profiling sessionsprofiles[n].save(filepath = 'node.cpuprofile')
: Save CPU profiling session\nto disk as JSONtakeHeapSnapshot(filepath = 'node.heapsnapshot')
: Take a heap snapshot\nand save to disk as JSONrun
: Run script (automatically runs on debugger's start)restart
: Restart scriptkill
: Kill scriptscripts
: List all loaded scriptsversion
: Display V8's versionV8 Inspector integration allows attaching Chrome DevTools to Node.js\ninstances for debugging and profiling. It uses the\nChrome DevTools Protocol.
\nV8 Inspector can be enabled by passing the --inspect
flag when starting a\nNode.js application. It is also possible to supply a custom port with that flag,\ne.g. --inspect=9222
will accept DevTools connections on port 9222.
To break on the first line of the application code, pass the --inspect-brk
\nflag instead of --inspect
.
$ node --inspect index.js\nDebugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29\nFor help, see: https://nodejs.org/en/docs/inspector\n
\n(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29\nat the end of the URL is generated on the fly, it varies in different\ndebugging sessions.)
\nIf the Chrome browser is older than 66.0.3345.0,\nuse inspector.html
instead of js_app.html
in the above URL.
Chrome DevTools doesn't support debugging worker threads yet.\nndb can be used to debug them.
", "type": "module", "displayName": "V8 inspector integration for Node.js" } ], "type": "misc", "displayName": "Advanced usage" } ] } ] }