Electron 主進(jìn)程調(diào)試

2018-10-03 18:23 更新

瀏覽器窗口的開(kāi)發(fā)工具僅能調(diào)試渲染器的進(jìn)程腳本(比如 web 頁(yè)面)。為了提供一個(gè)可以調(diào)試主進(jìn)程的方法,Electron 提供了 --debug--debug-brk 開(kāi)關(guān)。

命令行開(kāi)關(guān)

使用如下的命令行開(kāi)關(guān)來(lái)調(diào)試 Electron 的主進(jìn)程:

--debug=[port]

當(dāng)這個(gè)開(kāi)關(guān)用于 Electron 時(shí),它將會(huì)監(jiān)聽(tīng) V8 引擎中有關(guān) port 的調(diào)試器協(xié)議信息。默認(rèn)的 port5858。

--debug-brk=[port]

就像 --debug 一樣,但是會(huì)在第一行暫停腳本運(yùn)行。

使用 node-inspector 來(lái)調(diào)試

備注: Electron 目前對(duì) node-inspector 支持的不是特別好,如果你通過(guò) node-inspector 的 console 來(lái)檢查 process 對(duì)象,主進(jìn)程就會(huì)崩潰。

1. 確認(rèn)你已經(jīng)安裝了 node-gyp 所需工具

2. 安裝 node-inspector

$ npm install node-inspector

3. 安裝 node-pre-gyp 的一個(gè)修訂版

$ npm install git+https://git@github.com/enlight/node-pre-gyp.git#detect-electron-runtime-in-find

4. 為 Electron 重新編譯 node-inspector v8 模塊(將 target 參數(shù)修改為你的 Electron 的版本號(hào))

$ node_modules/.bin/node-pre-gyp --target=0.36.2 --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=0.36.2 --runtime=electron --fallback-to-build --directory node_modules/v8-profiler/ --dist-url=https://atom.io/download/atom-shell reinstall

[How to install native modules][how-to-install-native-modules].

5. 打開(kāi) Electron 的調(diào)試模式

你也可以用調(diào)試參數(shù)來(lái)運(yùn)行 Electron :

$ electron --debug=5858 your/app

或者,在第一行暫停你的腳本:

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

6. 使用 Electron 開(kāi)啟 node-inspector 服務(wù)

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

7. 加載調(diào)試器界面

在 Chrome 中打開(kāi) http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)