使用 node-inspector 进行主进程调试

尽管node-inspector 已经提供了一个熟悉的调试器 GUI 即可在 Chrome 中来调试 Electron 的主进程, 但因为 node-inspector 依赖于一些必须重新编译的原生 Node 模块.

所以你需要重编译所调试的Electron版本.你可以重建 node-inspector 依赖项, 或直接使用 electron-inspector 重建.在使用 electron-inspector时需要注意的是,有时它可能无法修补依赖关系.

使用 electron-inspector 进行调试

1. 安装 node-gyp required tools

2. 安装 electron-rebuild

  1. npm install electron-rebuild --save-dev

3. 安装 electron-inspector

  1. npm install electron-inspector --save-dev

4. 启动 Electron

--debug 参数来运行 Electron:

  1. electron --debug=5858 your/app

或者,在第一行暂停脚本:

  1. electron --debug-brk=5858 your/app

5. 启动 electron-inspector

在 macOS / Linux:

  1. node_modules/.bin/electron-inspector

在 Windows:

  1. node_modules\\.bin\\electron-inspector

electron-inspector 在首次运行时,或者更改了 Electron 的版本时需要重新编译 node-inspector 的依赖关系,重新编译的过程可能需要互联网连接,并花费一些时间才能下载 Node headers 和lib。

6. 加载调试器UI

在Chrome浏览器中打开http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858。如果以`--debug-brk`开头,您可能需要点击“暂停”才能强制更新UI。

U使用node-inspector进行调试

1. 安装 node-gyp required tools

2. 安装 node-inspector

  1. $ npm install node-inspector

3. 安装 node-pre-gyp

  1. $ npm install node-pre-gyp

4. 重新编译Electron的 node-inspector v8模块

注意: 将 target 参数修改为你的 Electron 的版本号

  1. $ node_modules/.bin/node-pre-gyp --target=1.2.5 --runtime=electron --fallback-to-build --directory node_modules/v8-debug/ --dist-url=https://atom.io/download/atom-shell reinstall
  2. $ node_modules/.bin/node-pre-gyp --target=1.2.5 --runtime=electron --fallback-to-build --directory node_modules/v8-profiler/ --dist-url=https://atom.io/download/atom-shell reinstall

详见 如何下载原生模块

5. 启用Electron的调试模式

您可以使用以下参数启动Electron:

  1. $ electron --debug=5858 your/app

或者,在第一行暂停脚本:

  1. $ electron --debug-brk=5858 your/app

6. 使用 Electron 开启 node-inspector 服务

  1. $ ELECTRON_RUN_AS_NODE=true path/to/electron.exe node_modules/node-inspector/bin/inspector.js

7. 加载调试器UI

在Chrome浏览器中打开http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858。如果从 --debug-brk开始查看输入行,则可能需要单击暂停。