The development tool devtools Electron browser window can only debug the JavaScript executed in this window (such as the web page). JavaScript executed in the main process, we need to use an external debugger. Electron provides --inspect and --inspect-brk switches to achieve this operation.

Command line switch

We can use the following command line switches to debug the main process of Electron

  • --inspect=[port] : When this switch is used Electron , it will listen V8 engine related port protocol information of the debugger, the default port value 5858 . For example, the following command:
electron --inspect=5858 .src/main.js
  • --inspect-brk=[port] : --inspector as 060c2d8c181c1a, but it will pause at the first line of the JavaScript

Main process debugging

.vscode/launch.json file in the root directory and use the following configuration:

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

Then VSCode , click on the settings in the upper right corner, and the newly created launch.json file will be opened, as shown below:

Then click the green triangle button in the upper left corner:

Then perform breakpoint debugging in the main.js


知否
221 声望177 粉丝

Skrike while the iron is hot.