跳转到主内容

调试应用

无论何时,您的Electron应用程序没有按照您设想的方式运行,一组调试工具也许可以帮助您找到代码的错误,性能瓶颈,或者优化的机会。

渲染进程​

最广泛使用来调试指定渲染进程的工具是Chromium的开发者工具集。 它可以获取到所有的渲染进程,包括BrowserWindow的实例,BrowserView以及WebView。 您可以通过编程的方式在BrowserWindow的webContents中调用openDevTool()API来打开它们:

const { BrowserWindow } = require('electron')

const win = new BrowserWindow()
win.webContents.openDevTools()

谷歌为他们的开发者工具提供了杰出的文档。 我们建议您熟悉它们,它们对于任何Electron开发者来说通常都是工具包中最强大的工具之一。

主进程​

调试主进程有点棘手, 因为您不能简单地打开开发者工具来调试它们。 The Chromium Developer Tools can be used to debug Electron's main process thanks to a closer collaboration between Google / Chrome and Node.js, but you might encounter oddities like require not being present in the console.

For more information, see the Debugging the Main Process documentation.

V8 崩溃​

如果V8环境崩溃的话, DevTools 将显示以下信息

DevTools was disconnected from the page.(DevTools 与页面断开连接) Once page is reloaded, DevTools will automatically reconnect.(页面重新载入后,DevTools 将自动重新连接)

Chromium日志可以通过ELECTRON_ENABLE_LOGGING环境变量启用。 For more information, see the environment variables documentation.

或者,可以传递命令行参数--enable-logging。 More information is available in the command line switches documentation.