Debugging an electron app in Visual Studio Code - node.js

Could I please ask for help with the following?
I have an electron app (basically the quick start app so very very simple).
I am viewing the code with Visual Studio Code. I run the project from the terminal window in Visual Studio Code with the command "npm start". All works fine.
I want be able to debug the electron code in main.js. So I clicked on "Run and Debug" and selected "Create a launch.json file". From the subsequent drop down I then selected "Node.js". This produces the launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\main.js"
}
]
}
If I now click on "launch Program" I get:
Error: Cannot find module 'electron'
I have electron installed globally, so I added this line:
"runtimeExecutable": "C:\\Users\\MyUserName\\AppData\\Roaming\\npm\\node_modules\\electron\\dist"
Now I get:
C:\Users\MyUserName\AppData\Roaming\npm\node_modules\electron\dist .\main.js
Error: spawn C:\Users\MyUserName\AppData\Roaming\npm\node_modules\electron\dist ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
at onErrorNT (internal/child_process.js:465:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Thanks for any advice that enables me to run and debug this via the "Run and Debug" button rather than just running it via "npm start" from the terminal window.

The solution I found is here: https://www.electronjs.org/docs/latest/tutorial/debugging-vscode
The launch.json that they give at that address is:
{
"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" : ["."],
"outputCapture": "std"
}
]
}
I copy and pasted the above in to my launch.json and my Electron app immediately started working in VS Code just like when I manually call 'npm start' from the console. Except it's better than just 'npm start' because for the server-side js files, debug breakpoints actually work!

Related

trying to debug electron app, and window not showing up

I am attempting to debug an electron app in vs code (the main process, not the renderer process). The project I am trying to debug is this repo:
https://github.com/hello-efficiency-inc/raven-reader
it works really nice, once you have it cloned, and the right things are installed. Then I can just run:
yarn electron:serve
And boom, it runs nicely.
But I would like to also debug this. So I look around a bit, and find that I should probably use this launch.json file in the .vscode directory:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Electron: Main",
"protocol": "inspector",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"runtimeArgs": ["--remote-debugging-port=9223", "."],
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
}
}
]
}
In my package.json, I have this line for finding the startfile:
"main": "./src/background.js",
So, I try to run by clicking the debug icon, and then green triangle where it says
run and debug | Electron:Main
Which gives me this in the call stack menu:
But nothing actually seems like it's running, I dont see any desktop window which I would like so I can actually debug, and I can't hit my breakpoints either
Best way to sort this is to install code runner extension in vscode

Getting ready to write my first C program, getting Error: spawn C:\WINDOWS\system32\cmd.exe ENOENT

I'm trying to get to the point that my "Hello World" will work, but am running into this error.
"C:\Program Files\nodejs\node.exe" "$C:/Users/Admin/Desktop/Coding/Hello World.c"
Error: spawn C:\WINDOWS\system32\cmd.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
I've set up my launch.json, and c_cpp_properties, but am not entirely sure I set them up correctly, as I am doing self-taught, and am having trouble finding useful examples online.
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "$C:/Users/Admin/Desktop/Coding/Hello World.c"
,"cwd": "$C:/Users/Admin/Desktop/Coding"
}
,{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "$C:/Users/Admin/Desktop/Coding/Hello World.c"
}
]
}
c_cpp_properties:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
Before anything: Do you have a C compiler in your machine? Or even Visual Studio? In order to compile C code using VS Code in Windows you need to have a compiler installed. VS Code does not have one built in. Visual Studio Code
If you do not have Visual Studio installed you need at least Microsoft C++ Build Tools or another windows C/C++ compiler, like gcc or CLang installed. Or all of them.
Then you configure VS Code to use then, editing the .json files and configuring the so called "build tasks'

VS Code Debugger does not kill node process after stopping the debugger

I am working on a node.js application using express.js as a web framework listening on PORT 3000.
I am using VS Code v1.46.
My launch.json file is
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\WebApi\\index.js",
"restart": true,
"protocol": "inspector"
}
]
}
I am able to start the debugging session for the first time, but 2nd time onwards, I get error Error: listen EADDRINUSE: address already in use :::3000
This error is because VSCode didn't terminate the node.exe process created in 1st debugging session and so in the subsequent session node failed to start the express server on port 3000 as it is still in used.
Can anyone help me to configure VSCode to terminate node.exe process once I stop the debugger?
This issue started just last week for me, not sure why, maybe windows update.
Any way I found the following :
https://github.com/OmniSharp/omnisharp-vscode/issues/2387
where they say that if you add to launch.json
"console": "externalTerminal" or "console": 'integratedTerminal'
it will open a console for the process so that u can kill manually
It works fine with the Using the "preview" debug extension.
This is the launch.json for using that mode, just make sure that you put the correct command of your package.json.
{
"version": "0.2.0",
"configurations": [
{
"type": "node-terminal",
"name": "Run Script: start",
"request": "launch",
"command": "npm run start",
"cwd": "${workspaceFolder}"
}
]
}

VSCode Nodejs debugger does not save changes?

I am just starting to learn using the Nodejs debugger. It has been really helpful already but I spent a a lot of time changing a js file I was debugging but I did not know that the changes I made were not being implement right away.
Is this an expected behavior? Can I set the debugger up so that it restarts on each save and notices the new changes?
Edit 1 :
Here is my debuf config:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app.js"
}
]
}
Normally, after every save, you have to restart your node program to see the changes. I'm sure there are other programs but one I use exclusively (I never use node alone) is nodemon. It's globally installed npm package program. Here is sample config for vs code debugger:
{
"type": "node",
"request": "launch",
"name": "Launch app - nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--inspect=9250"
],
"program": "${workspaceRoot}/api/app",
"cwd": "${workspaceRoot}/api",
"autoAttachChildProcesses": true,
"restart": true
},
nodemon will monitor all the files and restart node every time it detects changes.
Add runtimeArgs to change debugger port.

Visual studio code, debug not working

Hello i have problem with debug my app of node and visual studio code. When i tried run debug in visual i saw text " Debugger listening on port 30108
" but when i open my browser on localhost:30108 there is only information somethig like this
Type: connect
V8-Version: 4.5.103.36
Protocol-Version: 1
Embedding-Host: node v4.4.7
Content-Length: 0
on localhost:3000 (default app port) there is only error
" This site is unreachable "
So how to do, to be able to debug app with running app in browser ?
If you do a node app.js or npm start (or whatever for your project) in a terminal/command window, does your project also start successfully? Which OS are you using? Any firewall issues going on?
If you can access the site in your browser outside of VS Code, check my "Third attempt" documented here. Essentially, you need to edit both your launch.json and your package.json to indicate the port you are going to use. My examples follow the npm run {script name} format. You should be able to tailor it to suit.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm.cmd",
"runtimeArgs": [
"run", "start"
],
"port": 5858,
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
}
package.json
"scripts": {
"start": "node --inspect=5858 src/app.js",
}
Essentially, you need to ensure you are launching your app the same way you would from the command line. Then, ensure you have the matching port info in both files (and their respective locations) mentioned above.
More info available here for alternate/additional configuration options for debugging.

Resources