# 主進程調試
瀏覽器窗口的開發工具僅能調試渲染器的進程腳本(比如 web 頁面)。為了提供一個可以調試主進程
的方法,Electron 提供了 `--debug` 和 `--debug-brk` 開關。
## 命令行開關
使用如下的命令行開關來調試 Electron 的主進程:
### `--debug=[port]`
當這個開關用于 Electron 時,它將會監聽 V8 引擎中有關 `port` 的調試器協議信息。
默認的 `port` 是 `5858`。
### `--debug-brk=[port]`
就像 `--debug` 一樣,但是會在第一行暫停腳本運行。
## 使用 node-inspector 來調試
__備注:__ Electron 目前對 node-inspector 支持的不是特別好,
如果你通過 node-inspector 的 console 來檢查 `process` 對象,主進程就會崩潰。
### 1. 確認你已經安裝了 [node-gyp 所需工具](https://github.com/nodejs/node-gyp#installation)
### 2. 安裝 [node-inspector][node-inspector]
```bash
$ npm install node-inspector
```
### 3. 安裝 `node-pre-gyp` 的一個修訂版
```bash
$ npm install git+https://git@github.com/enlight/node-pre-gyp.git#detect-electron-runtime-in-find
```
### 4. 為 Electron 重新編譯 `node-inspector` `v8` 模塊(將 target 參數修改為你的 Electron 的版本號)
```bash
$ 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. 打開 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. 加載調試器界面
在 Chrome 中打開 http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858
[node-inspector]: https://github.com/node-inspector/node-inspector
- 介紹
- 常見問題
- Electron 常見問題
- 向導
- 支持平臺
- 分發應用
- 提交應用到 Mac App Store
- 打包應用
- 使用 Node 原生模塊
- 主進程調試
- 使用 Selenium 和 WebDriver
- 使用開發人員工具擴展
- 使用 Pepper Flash 插件
- 使用 Widevine CDM 插件
- 教程
- 快速入門
- 桌面環境集成
- 在線/離線事件探測
- API文檔
- 簡介
- 進程對象
- 支持的 Chrome 命令行開關
- 環境變量
- 自定義的 DOM 元素
- File 對象
- <webview> 標簽
- window.open 函數
- 在主進程內可用的模塊
- app
- autoUpdater
- BrowserWindow
- contentTracing
- dialog
- globalShortcut
- ipcMain
- Menu
- MenuItem
- powerMonitor
- powerSaveBlocker
- protocol
- session
- webContents
- Tray
- 在渲染進程(網頁)內可用的模塊
- desktopCapturer
- ipcRenderer
- remote
- webFrame
- 在兩種進程中都可用的模塊
- clipboard
- crashReporter
- nativeImage
- screen
- shell
- 開發
- 代碼規范
- 源碼目錄結構
- 與 NW.js(原 node-webkit)在技術上的差異
- 構建系統概覽
- 構建步驟(OS X)
- 構建步驟(Windows)
- 構建步驟(Linux)
- 在調試中使用 Symbol Server