浏览器窗口的DevTools只能调试渲染进程的脚本(好比Web页面)。为了提供一种方法来调试主进程中的脚本,Electron提供了 --debug 以及 --debug-brk 的选项开关。node
使用下列命令行切换到调试Electron的主进程模式:git
--debug=[port]
这个开关将使得Electron使用V8调试协议侦听在指定端口上。默认侦听端口是5858。github
--debug-brk=[port]
相似于 --debug 可是这个开关将在脚本的第一行暂停执行。shell
注意: 当前的Electron和node-inspector工做的不是很是好,而且当你在node-inspector的控制台检查 process 对象的时候主进程将会挂掉。npm
确信安装了node-gyp及其依赖的工具浏览器
安装node-inspectorbash
$ npm install node-inspector
安装 node-pre-gyp 补丁app
$ npm install git+https://git@github.com/enlight/node-pre-gyp.git#detect-electron-runtime-in-find
从新编译 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)
打开Electron调试模式工具
你能够使用一个debug标志来打开Electron,例如:
$ electron --debug=5858 your/app
或在脚本第一行暂停
$ electron --debug-brk=5858 your/app
使用electron启动node-inspector
$ ELECTRON_RUN_AS_NODE=true path/to/electron.exe node_modules/node-inspector/bin/inspector.js
装载调试器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/)
未经受权,禁止转载。