I am trying to debug a node add-on that is called within an electron application but I can not find a way to launch or attach an c debugger to the app. I have had success in debugging it when it's called by a simple node application, by calling node when launching the debugger, passing the js file as an argument (I used this tutorial: https://medium.com/@atulanand94/debugging-nodejs-c-addons-using-vs-code-27e9940fc3ad). Is there a way to launch the electron executable, passing the main code (index.js) of the app in a similar way?
Asked
Active
Viewed 1,041 times
5
-
Paulo this is a great question. Electron apps normally do start up by running a .js file in their "main" process. At development time, this is often specified in the package.json file, as part of an npm run start script. You can see this demonstrated on the Electron.js site in their getting started section. Can you include some more detail about what you have already tried, if you receive some kind of error message, and also if you are using an IDE like Visual Studio Code to debug? – GrahamMc Feb 19 '19 at 06:48
1 Answers
0
I found ways to do it for Xcode and Visual Studio.
First you need a small JavaScript file that imports your native add-on and calls the function you want to debug. This is the file to launch Electron with from the IDE. Let's assume this file is called debug.js
.
Xcode
- Open your project and select Edit Scheme for the target you want to debug.
- Under Run/Info/Executable, point it to
[path_to_your_project]/node_modules/electron/dist/Electron.app
. - Under Run/Arguments/Arguments Passed On Launch, add
[path_to_your_project]/debug.js
.
NB. It seems to fail if using ~
in the argument path, so make sure to spell out the whole path to the script, i.e. /Users/username/project/...
.
Visual Studio
- Open the solution, and then select Properties for the add-on project.
- Under Configuration Properties/Debugging/Command, enter
[path_to_your_project]/node_modules/electron/dist/electron.exe
. - Under Configuration Properties/Debugging/Command Arguments, enter
[path_to_your_project]/debug.js
.

Erik J
- 342
- 1
- 11
-
You can also, instead of using the special `debug.js` file, use your Electron app's standard `main.js` file as the argument. However, then you will only be able to debug native code that is called from Electron's main process. To debug native code that is called from a renderer process, you will need to launch the app first and then attach to one of the "Electron Helper" processes using your IDE's Attach to Process feature. Some trial and error might be required to find the right Electron Helper. – Erik J Sep 11 '20 at 10:17