- assert - 断言
- Buffer - 缓冲器
- child_process - 子进程
- cluster - 集群
- console - 控制台
- crypto - 加密
- dgram - 数据报
- dns - 域名服务器
- Error - 异常
- events - 事件
- fs - 文件系统
- global - 全局变量
- http - HTTP
- https - HTTPS
- module - 模块
- net - 网络
- os - 操作系统
- path - 路径
- process - 进程
- querystring - 查询字符串
- readline - 逐行读取
- repl - 交互式解释器
- stream - 流
- string_decoder - 字符串解码器
- timer - 定时器
- tls - 安全传输层
- tty - 终端
- url - 网址
- util - 实用工具
- v8 - V8引擎
- vm - 虚拟机
- zlib - 压缩
Node.js v10.8.0 文档
debugger 调试器#
Node.js 包含一个进程外的调试工具,可以通过V8检查器与内置的调试客户端访问。
要使用它,需要以 inspect 参数启动 Node.js,并带上需要调试的脚本的路径;然后会出现一个提示,表明已成功启动调试器:
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() => {
  3   console.log('world');
debug>
Node.js 的调试器客户端还未支持全部特性,但可以做些简单的步骤和检测。
在脚本的源代码中插入 debugger; 语句,则会在代码的那个位置启用一个断点:
// myscript.js
global.x = 5;
setTimeout(() => {
  debugger;
  console.log('世界');
}, 1000);
console.log('你好');
一旦运行调试器,则在第 3 行会出现一个断点:
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() => {
  3   debugger;
debug> cont
< 你好
break in myscript.js:3
  1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() => {
> 3   debugger;
  4   console.log('世界');
  5 }, 1000);
debug> next
break in myscript.js:4
  2 setTimeout(() => {
  3   debugger;
> 4   console.log('世界');
  5 }, 1000);
  6 console.log('你好');
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< 世界
break in myscript.js:5
  3   debugger;
  4   console.log('世界');
> 5 }, 1000);
  6 console.log('你好');
  7
debug> .exit
repl 命令用于运行代码。
next 命令用于步入下一行。
输入 help 可查看其他可用的命令。
按下 enter 键且不输入命令,则可重复上一个调试命令。
监视器#
可以在调试时监视表达式和变量的值。 在每个断点上,监视器列表中的每个表达式都会在当前上下文中被执行,并在断点的源代码列表之前立即显示。
输入 watch('my_expression') 开始监视表达式。
watchers 命令会打印已激活的监视器。
输入 unwatch('my_expression') 可以移除监视器。
命令参考手册#
步进#
- cont,- c- 继续执行。
- next,- n- 下一步。
- step,- s- 跳进函数。
- out,- o- 跳出函数。
- pause- 暂停运行代码(类似开发者工具中的暂停按钮)。
断点#
- setBreakpoint(),- sb()- 在当前行设置断点。
- setBreakpoint(line),- sb(line)- 在指定行设置断点。
- setBreakpoint('fn()'),- sb(...)- 在函数体的第一条语句设置断点。
- setBreakpoint('script.js', 1),- sb(...)- 在 script.js 的第 1 行设置断点。
- clearBreakpoint('script.js', 1),- cb(...)- 清除 script.js 中第 1 行的断点。
也可以在还未被加载的文件(模块)中设置断点:
$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/4e3db158-9791-4274-8909-914f7facf3bd
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in main.js:1
> 1 (function (exports, require, module, __filename, __dirname) { 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>
信息#
- backtrace,- bt- 打印当前执行框架的回溯。
- list(5)- 列出脚本源代码 5 行的上下文(前后各 5 行)。
- watch(expr)- 添加表达式到监视列表。
- unwatch(expr)- 从监视列表移除表达式。
- watchers- 列出所有监视器和它们的值(每个断点会自动列出)。
- repl- 打开调试器的 repl,用于在所调试的脚本的上下文中进行执行。
- exec expr- 在所调试的脚本的上下文中执行一个表达式。
执行控制#
- run- 运行脚本(调试器开始时自动运行)。
- restart- 重新启动脚本。
- kill- 终止脚本。
杂项#
- scripts- 列出所有已加载的脚本。
- version- 显示 V8 引擎的版本号。
高级用法#
Node.js的V8检查器集成#
V8 的检查器集成可以附加 Chrome 的开发者工具到 Node.js 实例以用于调试和性能分析。 它使用了 Chrome开发者工具协议。
当启动一个 Node.js 应用时,V8 检查器可以通过传入 --inspect 标志启用。
也可以通过该标志提供一个自定义的端口,如 --inspect=9222 会在 9222 端口接受开发者工具连接。
要想在应用代码的第一行断开,可以传入 --inspect-brk 标志而不是 --inspect。
$ node --inspect index.js
Debugger listening on 127.0.0.1:9229.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
(在上面的例子中,URL 后面的 UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 是在运行时生成的,在不同的调试会话中是不一样的。)