node.js 调试工具 node-inspector


This is the 10th in a series of posts leading up to Node.js Knockout on debugging node processes using Node Inspector. This post was written by Node Knockout judge and Node Inspector author Danny Coates.

While node has a built in debugger, Node Inspector provides a pleasant graphical interface for debugging node programs.

Node Inspector is a debugger interface for node.js using the WebKit Web Inspector, the familiar javascript debugger from Safari and Chrome.

Install

With npm:

npm install -g node-inspector

Enable debug mode

To use node-inspector, enable debugging on the node you wish to debug. You can either start node with a debug flag like:

$ node --debug your/node/program.js

or, to pause your script on the first line:

$ node --debug-brk your/short/node/script.js

NOTE: make sure that the --debug flag comes beforeyour/node/program.js or else you may see an EADDRINUSE error.

Or you can enable debugging on a node that is already running by sending it a signal:

  1. Get the PID of the node process using your favorite method.pgrep or ps -ef are good

    $ pgrep -l node
    2345 node your/node/server.js

  2. Send it the USR1 signal

    $ kill -s USR1 2345

Great! Now you’re ready to attach node-inspector.

Debugging

  1. start the inspector. I usually put it in the background

    $ node-inspector &

  2. open http://127.0.0.1:8080/debug?port=5858 in your favorite WebKit based browser

  3. you should now see the javascript source from node. If you don’t, click the scripts tab.

  4. select a script and set some breakpoints (far left line numbers) or simply add a debugger call in your code (node will break automatically on the call, just as V8 does).

  5. then watch the slightly outdated but hilarious screencasts

node-inspector works almost exactly like the web inspector in Safari and Chrome. Here’s a good overview of the UI.

FAQ

  1. I don’t see one of my script files in the file list.

    try refreshing the browser (F5 or ⌘-r or control-r)

  2. My script runs too fast to attach the debugger.

    add a debugger statement in your code where you want the debugger to stop

    or use --debug-brk to pause the script on the first line

  3. Can I debug remotely?

    Yes. node-inspector needs to run on the same machine as the node process, but your browser can be anywhere. Just make sure the firewall is open on 8080

  4. I got the ui in a weird state.

    when in doubt, refresh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值