ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 9.2.在 node-inspector 中调试主进程 [`node-inspector`][node-inspector] 提供了一个熟悉的开发者工具 GUI,可以用在 Chrome 中调试 Electron 主进程,然而,因为 `node-inspector` 依赖于一些原生 Node 模块,它们必须以你希望调试的版本的 Electron 为目标重新编译。你可以自己重新编译 `node-inspector` 的依赖关系,或者让 [`electron-inspector`][electron-inspector] 为你完成,这个文档覆盖这两种途径。 **注意**: 在编写的时候,最新版的 `node-inspector` (0.12.8)如果没有修复其中一个依赖关项时,不能以 Electron 1.3.0 或者更新的版本为目标重新编译。如果你使用 `electron-inspector` ,它会为你考虑这点。 ## 使用 `electron-inspector` 来调试 ### 1. 安装 [node-gyp required tools][node-gyp-required-tools] ### 2. 如果你还没有安装过,先安装 [`electron-rebuild`][electron-rebuild] ```shell npm install electron-rebuild --save-dev ``` ### 3. 安装 [`electron-inspector`][electron-inspector] ```shell npm install electron-inspector --save-dev ``` ### 4. 启动 Electron 使用 `--debug` 开关运行 Electron: ```shell electron --debug=5858 your/app ``` 或者,要暂停 JavaScript 的第一行的执行: ```shell electron --debug-brk=5858 your/app ``` ### 5. 启动 electron-inspector macOS / Linux: ```shell node_modules/.bin/electron-inspector ``` Windows: ```shell node_modules\\.bin\\electron-inspector ``` `electron-inspector` 在第一次运行时需要重建 `node-inspector` 依赖关系,包括任何时候你修改了你的 Electron 版本时。重建过程可能需要一个网络连接来下载 Node headers 和 库,可能花费一些时间。 ### 6. 载入调试工具 UI 在 Chrome 浏览器中打开 http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858 。如果使用 `--debug-brk` 启动,你可能需要点击暂停来强制更新 UI。 ## 使用 `node-inspector` 来调试 ### 1. 安装 [node-gyp required tools][node-gyp-required-tools] ### 2. 安装 [`node-inspector`][node-inspector] ```bash $ npm install node-inspector ``` ### 3. 安装 [`node-pre-gyp`][node-pre-gyp] ```bash $ npm install node-pre-gyp ``` ### 4. 为 Electron 重新编译 `node-inspector` `v8` 模块 **注意:** 更新目标参数为你的 Electron 版本号 ```bash $ 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 $ 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 ``` 也可以查看 [如何安装原生模块][how-to-install-native-modules]。 ### 5. 为 Electron 启用调试模式 也可以通过一个调试开关启动 Electron,如下: ```bash $ electron --debug=5858 your/app ``` 或者,在第一行暂停你的脚本: ```bash $ electron --debug-brk=5858 your/app ``` ### 6. 使用 Electron 启动 [`node-inspector`][node-inspector] 服务 ```bash $ 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` 启动,必须点击暂停查看入口行。 * [electron-inspector]: https://github.com/enlight/electron-inspector * [electron-rebuild]: https://github.com/electron/electron-rebuild * [node-inspector]: https://github.com/node-inspector/node-inspector * [node-pre-gyp]: https://github.com/mapbox/node-pre-gyp * [node-gyp-required-tools]: https://github.com/nodejs/node-gyp#installation * [how-to-install-native-modules]: 【使用Node 原生模块】