node.js 调试工具

本文介绍了Node.js自带的调试器使用方法,包括如何启动调试模式、设置断点、单步执行代码及查看变量值等基本操作。通过实例演示了如何在脚本中启用断点并进行调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 v8 自带的调试器,用法:node debug app.js,即可进入调试流程

Debugger 调试器

V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear:

V8引擎自身配备了全面的调试器,该调试器通过简单的TCP协议在进程外访问。Node中内置了该调试器的客户端,要使用它可以在启动Node时附加debug参数,此模式下将显示debug提示符:

% node debug myscript.js
debug>

At this point myscript.js is not yet running. To start the script, enter the command run. If everything works okay, the output should look like this:

此时 myscript.js还没有开始执行,若要执行这段脚本,还需要输入run命令。如果一切运行正常的话输出信息应该如下所示:

% node debug myscript.js
debug> run
debugger listening on port 5858
connecting...ok

Node's debugger client doesn't support the full range of commands, but simple step and inspection is possible. By putting the statement debugger; into the source code of your script, you will enable a breakpoint.

Node的调试器客户端虽然没有支持所有的命令,但是实现简单的单步调试还是可以的。你可以在脚本代码中声明debugger;语句从而启用一个断点。

For example, suppose myscript.js looked like this:

例如,假设myscript.js代码如下:

// myscript.js
x = 5;
setTimeout(function () {
  debugger;
  console.log("world");
}, 1000);
console.log("hello");

Then once the debugger is run, it will break on line 4.

一旦调试器开始运行,那么它将在执行到第4行代码处时停止。

% ./node debug myscript.js
debug> run
debugger listening on port 5858
connecting...ok
hello
break in #<an Object>._onTimeout(), myscript.js:4
  debugger;
  ^
debug> next
break in #<an Object>._onTimeout(), myscript.js:5
  console.log("world");
  ^
debug> print x
5
debug> print 2+2
4
debug> next
world
break in #<an Object>._onTimeout() returning undefined, myscript.js:6
}, 1000);
^
debug> quit
A debugging session is active. Quit anyway? (y or n) y
%

The print command allows you to evaluate variables. The next command steps over to the next line. There are a few other commands available and more to come type help to see others.

print命令允许将变量输出到控制台进行查看。next命令单步调试到下一行代码。还有其他一些可用的命令你可以通过输入help进行查看。

Advanced Usage 高级用法

The V8 debugger can be enabled and accessed either by starting Node with the --debug command-line flag or by signaling an existing Node process with SIGUSR1.

要启用V8引擎调试器你可以在启动Node时增加命令行参数--debug或者给一个已经存在的Node进程发送值为SIGUSR1的信号量。

参考:1  http://docs.cnodejs.net/cman/debugger.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值