偵錯器#

穩定性:2 - 穩定

Node.js 包含一個命令列偵錯工具。Node.js 偵錯器用戶端並非功能齊全的偵錯器,但可以進行簡單的逐步執行和檢查。

若要使用它,請以 inspect 參數啟動 Node.js,後接要偵錯的腳本路徑。

$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
< For help, see: https://node.dev.org.tw/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
 ok
Break on start in myscript.js:2
  1 // myscript.js
> 2 global.x = 5;
  3 setTimeout(() => {
  4   debugger;
debug> 

偵錯器會自動在第一個可執行行中中斷。若要改為執行到第一個中斷點(由 debugger 陳述式指定),請將 NODE_INSPECT_RESUME_ON_START 環境變數設定為 1

$ cat myscript.js
// myscript.js
global.x = 5;
setTimeout(() => {
  debugger;
  console.log('world');
}, 1000);
console.log('hello');
$ NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/f1ed133e-7876-495b-83ae-c32c6fc319c2
< For help, see: https://node.dev.org.tw/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
< hello
<
break in myscript.js:4
  2 global.x = 5;
  3 setTimeout(() => {
> 4   debugger;
  5   console.log('world');
  6 }, 1000);
debug> next
break in myscript.js:5
  3 setTimeout(() => {
  4   debugger;
> 5   console.log('world');
  6 }, 1000);
  7 console.log('hello');
debug> repl
Press Ctrl+C to leave debug repl
> x
5
> 2 + 2
4
debug> next
< world
<
break in myscript.js:6
  4   debugger;
  5   console.log('world');
> 6 }, 1000);
  7 console.log('hello');
  8
debug> .exit
$ 

repl 命令允許遠端評估程式碼。next 命令會逐步執行到下一行。輸入 help 以查看有哪些其他可用命令。

在未輸入命令的情況下按下 enter 會重複先前的偵錯器命令。

觀察器#

在偵錯期間,可以觀察表達式和變數值。在每個中斷點上,觀察器清單中的每個表達式都會在當前內容中評估,並在中斷點的原始碼清單之前立即顯示。

若要開始觀察表達式,請輸入 watch('my_expression')watchers 命令會列印出活動的觀察器。若要移除觀察器,請輸入 unwatch('my_expression')

命令參考#

逐步執行#

  • contc:繼續執行
  • nextn:逐步執行下一步
  • steps:逐步執行
  • outo:跳出
  • pause:暫停執行中的程式碼(類似開發人員工具中的暫停按鈕)

中斷點#

  • setBreakpoint()sb():設定目前行中斷點
  • setBreakpoint(line)sb(line):設定特定行中斷點
  • setBreakpoint('fn()')sb(...):設定函式主體中第一個陳述式中斷點
  • setBreakpoint('script.js', 1)sb(...):設定 script.js 第一行中斷點
  • setBreakpoint('script.js', 1, 'num < 4')sb(...):設定 script.js 第一行條件中斷點,僅在 num < 4 評估為 true 時中斷
  • clearBreakpoint('script.js', 1)cb(...):清除 script.js 第 1 行中斷點

也可以設定中斷點在尚未載入的檔案(模組)中

$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
< For help, see: https://node.dev.org.tw/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
Break on start in main.js:1
> 1 const mod = require('./mod.js');
  2 mod.hello();
  3 mod.hello();
debug> setBreakpoint('mod.js', 22)
Warning: script 'mod.js' was not loaded yet.
debug> c
break in mod.js:22
 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 21
>22 exports.hello = function() {
 23   return 'hello from module';
 24 };
debug> 

也可以設定條件中斷點,僅在給定表達式評估為 true 時中斷

$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
< For help, see: https://node.dev.org.tw/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
Break on start in main.js:7
  5 }
  6
> 7 addOne(10);
  8 addOne(-1);
  9
debug> setBreakpoint('main.js', 4, 'num < 0')
  1 'use strict';
  2
  3 function addOne(num) {
> 4   return num + 1;
  5 }
  6
  7 addOne(10);
  8 addOne(-1);
  9
debug> cont
break in main.js:4
  2
  3 function addOne(num) {
> 4   return num + 1;
  5 }
  6
debug> exec('num')
-1
debug> 

資訊#

  • backtracebt:列印目前執行架構的回溯
  • list(5):列出腳本原始碼,附帶 5 行背景(前後各 5 行)
  • watch(expr):將表達式加入監控清單
  • unwatch(expr):從監控清單中移除表達式
  • unwatch(index):從監控清單中移除特定索引的表達式
  • watchers:列出所有監控和其值(在每個中斷點自動列出)
  • repl:開啟偵錯器的 repl,用於在偵錯腳本的背景中評估
  • exec exprp expr:在偵錯腳本的背景中執行表達式並列印其值
  • profile:開始 CPU 分析時間
  • profileEnd:停止目前的 CPU 分析時間
  • profiles:列出所有已完成的 CPU 分析時間
  • profiles[n].save(filepath = 'node.cpuprofile'):將 CPU 分析記錄儲存到磁碟中,格式為 JSON
  • takeHeapSnapshot(filepath = 'node.heapsnapshot'):擷取堆快照並儲存到磁碟中,格式為 JSON

執行控制#

  • run:執行腳本(在偵錯器啟動時自動執行)
  • restart:重新啟動腳本
  • kill:終止腳本

其他#

  • scripts:列出所有已載入的腳本
  • version:顯示 V8 的版本

進階用法#

V8 檢查器整合到 Node.js#

V8 檢查器整合功能允許將 Chrome DevTools 附加到 Node.js 實例,以進行偵錯和分析。它使用 Chrome DevTools 通訊協定

啟動 Node.js 應用程式時,傳遞 --inspect 旗標可以啟用 V8 檢查器。也可以使用該旗標提供自訂埠,例如 --inspect=9222 會在埠 9222 上接受 DevTools 連線。

若要在應用程式程式碼的第一行中中斷,請傳遞 --inspect-brk 旗標,而不是 --inspect

$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
For help, see: https://node.dev.org.tw/en/docs/inspector 

(在上述範例中,網址結尾的 UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 是動態產生的,在不同的偵錯記錄中會有所不同。)

如果 Chrome 瀏覽器的版本低於 66.0.3345.0,請在上述網址中使用 inspector.html,而不是 js_app.html

Chrome DevTools 目前不支援偵錯 工作執行緒。可以使用 ndb 來偵錯工作執行緒。