跳转到主内容

使用 VsCode调试

This guide goes over how to set up VSCode debugging for both your own Electron project as well as the native Electron codebase.

调试您的 Electron 应用

主进程

1. 在 VSCode 中打开一个 Electron 项目。

$ npx create-electron-app@latest my-app
$ code my-app

2. 添加文件 .vscode/launch.json,内容为

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"args" : ["."]
}
]
}

3. 调试

Set some breakpoints in main.js, and start debugging in the Debug View. You should be able to hit the breakpoints.

调试 Electron 代码库(Electron codebase)

If you want to build Electron from source and modify the native Electron codebase, this section will help you in testing your modifications.

For those unsure where to acquire this code or how to build it, Electron's Build Tools automates and explains most of this process. If you wish to manually set up the environment, you can instead use these build instructions.

Windows (C++)

1. 在 VSCode 中打开一个 Electron 项目。

$ npx create-electron-app@latest my-app
$ code my-app

2. 添加文件 .vscode/launch.json,内容为

{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\out\\your-executable-location\\electron.exe",
"args": ["your-electron-project-path"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{"name": "ELECTRON_ENABLE_LOGGING", "value": "true"},
{"name": "ELECTRON_ENABLE_STACK_DUMPING", "value": "true"},
{"name": "ELECTRON_RUN_AS_NODE", "value": ""},
],
"externalConsole": false,
"sourceFileMap": {
"o:\\": "${workspaceFolder}",
},
},
]
}

配置说明

  • cppvsdbg requires the built-in C/C++ extension be enabled.
  • ${workspaceFolder} 是 Chromium 的 的完整路径。
  • your-executable-location 将是以下几项之一:
    • Testing: If you are using the default settings of Electron's Build-Tools or the default instructions when building from source.
    • Release:如果你构建了一个发布版本,而不是测试版本。
    • your-directory-name: If you modified this during your build process from the default, this will be whatever you specified.
  • The args array string "your-electron-project-path" should be the absolute path to either the directory or main.js file of the Electron project you are using for testing. In this example, it should be your path to my-app.

3. 调试

Set some breakpoints in the .cc files of your choosing in the native Electron C++ code, and start debugging in the Debug View.