nodejs學習筆記Node.js 調試命令

來源:互聯網
上載者:User

標籤:技術   rip   connect   nbsp   home   開啟   用戶端   快速入門   bottom   

3.4  調試        47 

下面是一個簡單的例子: 

$ node debug debug.js 

< debugger listening on port 5858 connecting... ok 

break in /home/byvoid/debug.js:11 var a = 1;

2 var b = ‘world‘; 

3 var c = function (x) { debug> n 

break in /home/byvoid/debug.js:21 var a = 1;

2 var b = ‘world‘; 

3 var c = function (x) { 

4    console.log(‘hello ‘ + x + a); debug> sb(‘debug.js‘, 4) 

1  var a = 1; 

2  var b = ‘world‘; 

3  var c = function (x) { 

*  4   console.log(‘hello ‘ + x + a); 

5  }; 

6  c(b); 

7  }); 

debug> c 

break in /home/byvoid/debug.js:42 var b = ‘world‘;

3 var c = function (x) { 

* 4   console.log(‘hello ‘ + x + a); 

5  }; 

6  c(b); debug> repl 

Press Ctrl + C to leave debug repl 

>  x 

‘world‘ > a + 1 2 

debug> c 

< hello world1 program terminated

 

 

3.4.2  遠端偵錯 

V8 提供的調試功能是基於 TCP 協議的,因此 Node.js 可以輕鬆地實現遠端偵錯。在命 令行下使用以下兩個語句之一可以開啟調試伺服器: 

node

--debug[=port] script.js

10

node

--debug-brk[=port] script.js

 

48        第 3 章    Node.js 快速入門 

node --debug 命令選項可以啟動調試伺服器,預設情況下調試連接埠是 5858,也可以 使用 --debug=1234 指定調試連接埠為 1234。使用 --debug 選項運行指令碼時,指令碼會正常 執行,但不會暫停,在執行過程中調試用戶端可以串連到調試伺服器。如果要求指令碼暫停執 行等待用戶端串連,則應該使用 --debug-brk 選項。這時調試伺服器在啟動後會立刻暫停 執行指令碼,等待調試用戶端串連。 

當調試伺服器啟動以後,可以用命令列調試工具作為調試用戶端串連,例如: 

//在一個終端中 

$ node --debug-brk debug.js 

debugger listening on port 5858 

//在另一個終端中 

$ node debug 127.0.0.1:5858 connecting... okdebug> n 

break in /home/byvoid/debug.js:21 var a = 1;2 var b = ‘world‘; 

3 var c = function (x) { 

4 console.log(‘hello ‘ + x + a); debug> 

事實上,當使用 node debug debug.js 命令調試時,只不過是用 Node.js 命令列工 具將以上兩步工作自動完成而已。

nodejs學習筆記Node.js 調試命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.