electron/docs/tutorial/debugging-main-process.md
Pine c45ca254e3 Add instruction for debugging main process in VSCode
I hope you don't mind me putting the VSCode part before `node-inspector`, because VSCode seems easier to setup, and VSCode could inspect `process` without crashing.
2016-09-19 22:43:05 -07:00

3.5 KiB

Debugging the Main Process

The browser window DevTools can only debug the renderer process scripts (i.e. the web pages). In order to provide a way to debug the scripts from the main process, Electron has provided the --debug and --debug-brk switches.

Command Line Switches

Use the following command line switches to debug Electron's main process:

--debug=[port]

When this switch is used Electron will listen for V8 debugger protocol messages on the port. The default port is 5858.

--debug-brk=[port]

Like --debug but pauses the script on the first line.

Use VSCode for Debugging

1. Open an Electron project in VSCode.

$ git clone git@github.com:electron/electron-quick-start.git
$ code electron-quick-start

2. Add a file .vscode/launch.json with the following configuration:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Main Process",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
      "program": "${workspaceRoot}/main.js"
    }
  ]
}

Note: For Windows, use "${workspaceRoot}/node_modules/.bin/electron.cmd" for runtimeExecutable.

3. Debugging

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

Here is a pre-configured project that you can download and directly debug in VSCode: https://github.com/octref/vscode-electron-debug/tree/master/electron-quick-start

Use node-inspector for Debugging

Note: Electron doesn't currently work very well with node-inspector, and the main process will crash if you inspect the process object under node-inspector's console.

1. Install the node-gyp required tools

2. Install node-inspector

$ npm install node-inspector

3. Install node-pre-gyp

$ npm install node-pre-gyp

4. Recompile the node-inspector v8 modules for Electron

Note: Update the target argument to be your Electron version number

$ node_modules/.bin/node-pre-gyp --target=1.2.5 --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=1.2.5 --runtime=electron --fallback-to-build --directory node_modules/v8-profiler/ --dist-url=https://atom.io/download/atom-shell reinstall

See also How to install native modules.

5. Enable debug mode for Electron

You can either start Electron with a debug flag like:

$ electron --debug=5858 your/app

or, to pause your script on the first line:

$ electron --debug-brk=5858 your/app

6. Start the node-inspector server using Electron

$ ELECTRON_RUN_AS_NODE=true path/to/electron.exe node_modules/node-inspector/bin/inspector.js

7. Load the debugger UI

Open http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858 in the Chrome browser. You may have to click pause if starting with debug-brk to see the entry line.