• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{
2  "type": "module",
3  "source": "doc/api/debugger.md",
4  "introduced_in": "v0.9.12",
5  "stability": 2,
6  "stabilityText": "Stable",
7  "miscs": [
8    {
9      "textRaw": "Debugger",
10      "name": "Debugger",
11      "introduced_in": "v0.9.12",
12      "stability": 2,
13      "stabilityText": "Stable",
14      "type": "misc",
15      "desc": "<p>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.</p>\n<p>To use it, start Node.js with the <code>inspect</code> argument followed by the path to the\nscript to debug.</p>\n<pre><code class=\"language-console\">$ node inspect myscript.js\n&#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8\n&#x3C; For help, see: https://nodejs.org/en/docs/inspector\n&#x3C;\nconnecting to 127.0.0.1:9229 ... ok\n&#x3C; Debugger attached.\n&#x3C;\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</code></pre>\n<p>The debugger automatically breaks on the first executable line. To instead\nrun until the first breakpoint (specified by a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger\"><code>debugger</code></a> statement), set\nthe <code>NODE_INSPECT_RESUME_ON_START</code> environment variable to <code>1</code>.</p>\n<pre><code class=\"language-console\">$ 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&#x3C; Debugger listening on ws://127.0.0.1:9229/f1ed133e-7876-495b-83ae-c32c6fc319c2\n&#x3C; For help, see: https://nodejs.org/en/docs/inspector\n&#x3C;\nconnecting to 127.0.0.1:9229 ... ok\n&#x3C; Debugger attached.\n&#x3C;\n&#x3C; hello\n&#x3C;\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&#x3C; world\n&#x3C;\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</code></pre>\n<p>The <code>repl</code> command allows code to be evaluated remotely. The <code>next</code> command\nsteps to the next line. Type <code>help</code> to see what other commands are available.</p>\n<p>Pressing <code>enter</code> without typing a command will repeat the previous debugger\ncommand.</p>",
16      "miscs": [
17        {
18          "textRaw": "Watchers",
19          "name": "watchers",
20          "desc": "<p>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.</p>\n<p>To begin watching an expression, type <code>watch('my_expression')</code>. The command\n<code>watchers</code> will print the active watchers. To remove a watcher, type\n<code>unwatch('my_expression')</code>.</p>",
21          "type": "misc",
22          "displayName": "Watchers"
23        },
24        {
25          "textRaw": "Command reference",
26          "name": "command_reference",
27          "modules": [
28            {
29              "textRaw": "Stepping",
30              "name": "stepping",
31              "desc": "<ul>\n<li><code>cont</code>, <code>c</code>: Continue execution</li>\n<li><code>next</code>, <code>n</code>: Step next</li>\n<li><code>step</code>, <code>s</code>: Step in</li>\n<li><code>out</code>, <code>o</code>: Step out</li>\n<li><code>pause</code>: Pause running code (like pause button in Developer Tools)</li>\n</ul>",
32              "type": "module",
33              "displayName": "Stepping"
34            },
35            {
36              "textRaw": "Breakpoints",
37              "name": "breakpoints",
38              "desc": "<ul>\n<li><code>setBreakpoint()</code>, <code>sb()</code>: Set breakpoint on current line</li>\n<li><code>setBreakpoint(line)</code>, <code>sb(line)</code>: Set breakpoint on specific line</li>\n<li><code>setBreakpoint('fn()')</code>, <code>sb(...)</code>: Set breakpoint on a first statement in\nfunction's body</li>\n<li><code>setBreakpoint('script.js', 1)</code>, <code>sb(...)</code>: Set breakpoint on first line of\n<code>script.js</code></li>\n<li><code>setBreakpoint('script.js', 1, 'num &#x3C; 4')</code>, <code>sb(...)</code>: Set conditional\nbreakpoint on first line of <code>script.js</code> that only breaks when <code>num &#x3C; 4</code>\nevaluates to <code>true</code></li>\n<li><code>clearBreakpoint('script.js', 1)</code>, <code>cb(...)</code>: Clear breakpoint in <code>script.js</code>\non line 1</li>\n</ul>\n<p>It is also possible to set a breakpoint in a file (module) that\nis not loaded yet:</p>\n<pre><code class=\"language-console\">$ node inspect main.js\n&#x3C; Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9\n&#x3C; For help, see: https://nodejs.org/en/docs/inspector\n&#x3C;\nconnecting to 127.0.0.1:9229 ... ok\n&#x3C; Debugger attached.\n&#x3C;\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</code></pre>\n<p>It is also possible to set a conditional breakpoint that only breaks when a\ngiven expression evaluates to <code>true</code>:</p>\n<pre><code class=\"language-console\">$ node inspect main.js\n&#x3C; Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35\n&#x3C; For help, see: https://nodejs.org/en/docs/inspector\n&#x3C;\nconnecting to 127.0.0.1:9229 ... ok\n&#x3C; 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 &#x3C; 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</code></pre>",
39              "type": "module",
40              "displayName": "Breakpoints"
41            },
42            {
43              "textRaw": "Information",
44              "name": "information",
45              "desc": "<ul>\n<li><code>backtrace</code>, <code>bt</code>: Print backtrace of current execution frame</li>\n<li><code>list(5)</code>: List scripts source code with 5 line context (5 lines before and\nafter)</li>\n<li><code>watch(expr)</code>: Add expression to watch list</li>\n<li><code>unwatch(expr)</code>: Remove expression from watch list</li>\n<li><code>unwatch(index)</code>: Remove expression at specific index from watch list</li>\n<li><code>watchers</code>: List all watchers and their values (automatically listed on each\nbreakpoint)</li>\n<li><code>repl</code>: Open debugger's repl for evaluation in debugging script's context</li>\n<li><code>exec expr</code>, <code>p expr</code>: Execute an expression in debugging script's context and\nprint its value</li>\n<li><code>profile</code>: Start CPU profiling session</li>\n<li><code>profileEnd</code>: Stop current CPU profiling session</li>\n<li><code>profiles</code>: List all completed CPU profiling sessions</li>\n<li><code>profiles[n].save(filepath = 'node.cpuprofile')</code>: Save CPU profiling session\nto disk as JSON</li>\n<li><code>takeHeapSnapshot(filepath = 'node.heapsnapshot')</code>: Take a heap snapshot\nand save to disk as JSON</li>\n</ul>",
46              "type": "module",
47              "displayName": "Information"
48            },
49            {
50              "textRaw": "Execution control",
51              "name": "execution_control",
52              "desc": "<ul>\n<li><code>run</code>: Run script (automatically runs on debugger's start)</li>\n<li><code>restart</code>: Restart script</li>\n<li><code>kill</code>: Kill script</li>\n</ul>",
53              "type": "module",
54              "displayName": "Execution control"
55            },
56            {
57              "textRaw": "Various",
58              "name": "various",
59              "desc": "<ul>\n<li><code>scripts</code>: List all loaded scripts</li>\n<li><code>version</code>: Display V8's version</li>\n</ul>",
60              "type": "module",
61              "displayName": "Various"
62            }
63          ],
64          "type": "misc",
65          "displayName": "Command reference"
66        },
67        {
68          "textRaw": "Advanced usage",
69          "name": "advanced_usage",
70          "modules": [
71            {
72              "textRaw": "V8 inspector integration for Node.js",
73              "name": "v8_inspector_integration_for_node.js",
74              "desc": "<p>V8 Inspector integration allows attaching Chrome DevTools to Node.js\ninstances for debugging and profiling. It uses the\n<a href=\"https://chromedevtools.github.io/devtools-protocol/\">Chrome DevTools Protocol</a>.</p>\n<p>V8 Inspector can be enabled by passing the <code>--inspect</code> flag when starting a\nNode.js application. It is also possible to supply a custom port with that flag,\ne.g. <code>--inspect=9222</code> will accept DevTools connections on port 9222.</p>\n<p>To break on the first line of the application code, pass the <code>--inspect-brk</code>\nflag instead of <code>--inspect</code>.</p>\n<pre><code class=\"language-console\">$ 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</code></pre>\n<p>(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.)</p>\n<p>If the Chrome browser is older than 66.0.3345.0,\nuse <code>inspector.html</code> instead of <code>js_app.html</code> in the above URL.</p>\n<p>Chrome DevTools doesn't support debugging <a href=\"worker_threads.html\">worker threads</a> yet.\n<a href=\"https://github.com/GoogleChromeLabs/ndb/\">ndb</a> can be used to debug them.</p>",
75              "type": "module",
76              "displayName": "V8 inspector integration for Node.js"
77            }
78          ],
79          "type": "misc",
80          "displayName": "Advanced usage"
81        }
82      ]
83    }
84  ]
85}