Electron指南 - 調試主進程

調試主進程

瀏覽器窗口的DevTools只能調試渲染進程的腳本(好比Web頁面)。爲了提供一種方法來調試主進程中的腳本,Electron提供了 --debug 以及 --debug-brk 的選項開關。node

命令行開關

使用下列命令行切換到調試Electron的主進程模式:git

--debug=[port]

這個開關將使得Electron使用V8調試協議偵聽在指定端口上。默認偵聽端口是5858。github

--debug-brk=[port]

相似於 --debug 可是這個開關將在腳本的第一行暫停執行。shell

使用node-inspector來調試

注意: 當前的Electron和node-inspector工做的不是很是好,而且當你在node-inspector的控制檯檢查 process 對象的時候主進程將會掛掉。npm

  1. 確信安裝了node-gyp及其依賴的工具瀏覽器

  2. 安裝node-inspectorbash

    $ npm install node-inspector
  3. 安裝 node-pre-gyp 補丁app

    $ npm install git+https://git@github.com/enlight/node-pre-gyp.git#detect-electron-runtime-in-find
  4. 從新編譯 node-inspector V8 模塊 (改變編譯目標至你的Electron版本號)electron

    $ node_modules/.bin/node-pre-gyp --target=0.36.11 --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.11 --runtime=electron --fallback-to-build --directory node_modules/v8-profiler/ --dist-url=https://atom.io/download/atom-shell reinstall
    查閱[如何安裝本地模塊](http://electron.atom.io/docs/tutorial/using-native-node-modules#how-to-install-native-modules)
  5. 打開Electron調試模式工具

    你能夠使用一個debug標誌來打開Electron,例如:
    $ electron --debug=5858 your/app
    或在腳本第一行暫停
    $ electron --debug-brk=5858 your/app
  6. 使用electron啓動node-inspector

    $ 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來啓動的話你必須點擊暫停來看的看到完整的行。

本文翻譯自[這裏] (http://electron.atom.io/docs/tutorial/debugging-main-process/)
未經受權,禁止轉載。

更多文章請瀏覽個人博客:@gihub, @coding

相關文章
相關標籤/搜索