I started with developing an app for homey and it goes quite well. I’m using vscode for the code writing and installed nodejs and the homey cli. I’m able to build and app with a device and driver which now runs on my Homey. I can see some log items etc appearing in the terminal which is all fine. I’m quite a newbie with developing but I do know the basics. But I’m missing the part of debugging code, is it possible to step into the code with breakpoints while running the app in homey? If so, how can it be done?
You can add the following line of code to the top of app.js
:
require('inspector').open(9229, '0.0.0.0')
That will start the V8 inspector when the app gets started, and you can interact with it from a regular Chrome browser (open chrome://inspect/
and it should be able to find it).
I have no idea if/how this works in combination with VScode. Also, make sure to remove that line from the code once you’re ready to publish the app.
I use these 4 lines of code (app.js) in each of my apps, so you can debug in VScode (remote) and then you don’t have any problems publishing the app
// Start debuger
if (process.env.DEBUG === “1”) {
require(“inspector”).open(9229, “0.0.0.0”, false);
//require(“inspector”).open(9229, “0.0.0.0”, true);
}
Could you explain more about setting up VSCode?
This is what I have tried:
I have added require('inspector').open(9222, '0.0.0.0')
I have selected the configuration as Node.js: Attach to remote program,
I entered the address as 0.0.0.0 and set the port to 9222
but when I run the debugger it eventually comes up with:
launch.json settings
{
"type": "node",
"request": "attach",
"name": "Attach to Homey",
"address": "192.xx.xx.xx", <- homey ip-address
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
}
Perfect, thank you.
Just one small note about
This has smart quotes so it can’t be just be copied and pasted. So for anyone that wants to use it, replace all the “ with ’
If only I had know all this before it would have saved me so much time.
So a massive thanks to @robertklep and @ChrisG
Never mind, I use "
The editor will automatically replace any regular quotes with their “smart” counterparts if used in regular text.
For pasting pieces of code, use the “Preformatted text” button:
I’m not sure if I was clear, but what I meant was I copied and pasted the example code into my file using VSCode, but when I ran the app it crashed because it didn’t understand the " characters that were pasted in. After I replaced all the " with ’ it works fine. Maybe it is a strange quirk with the way the text is placed in the clipboard when copied from the forum.
It took me a while to work out what the problem was as the error message in the crash just said the option was invalid.
Hi All, thank for the information, it helps a lot.
It appears that I’m able to attache the remote debugger from VS code. I changed the launch.json code in the directory of the app to the code above and pressed F5 with node.js and after that I run the app with ‘homey app run’, I got message that the debugger is attched etc and can also see the callstack. But when I set breakpoints in the explorer view de dots become grey and I’m not able to step into the code. What am I missing here? Sorry for the noob questions
EDIT:
Nevermind, when posted this I saw the problem, I replace the compleet json file instead of adding the setting. It now works.
So:
Edit the json.
Open the run tab
Run the “Attach to homey”
start the app: “homey run app”
Thanks all!
Great help, but when I try to start the app “homey run app” from inside VScode it tells me “cannot be loaded because running scripts is disabled on this system”… what am I doing wrong?
It should be ‘homey app run’. Simple typo by op.
I read about this in the Slack #Developers channel a while back.
I improved upon it and it can now launch the Homey app and attach the debugger automatically:
Only thing I’m still hoping to achieve is having the ‘attach’ tab open automatically. Best thing for now is to wait until the floating debugger bar (The thing they complain about here) shows a drop-down menu. Then you can switch in the debugger tab to the ‘attach’ tab.
@ChrisG came with good info when developing using a container:
If you test with a container the “require(‘inspector’).open(9222, ‘0.0.0.0’, true);” does not work anymore, because the debugger is already started. Then you have to use the method “require(‘inspector’).waitForDebugger();“.
And @DaneedeKruyff mentiones this script:
"version": "0.2.0",
"configurations": [{
"name": "Launch app",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "homey",
"args": ["app", "run", "--remote"],
"outputCapture": "std",
"serverReadyAction": {
"pattern": "Debugger listening on",
"action": "startDebugging",
"name": "Attach to Homey"
}
},
{
"name": "Attach to Homey",
"type": "node",
"request": "attach",
"address": "192.168.1.191",
"port": 9222,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
}
]
}
I have this one, this includes the --remote
One thing to note, if you use homey app run --remote
on the Homey 2023 then change:
"remoteRoot": "/"
to
"remoteRoot": "/app/"
Another hint…if you want to debug on Homey and Docker, you can add this code to your app.js:
if (process.env.DEBUG === '1'){
try{
require('inspector').waitForDebugger();
}
catch(error){
require('inspector').open(9225, '0.0.0.0', true);
}
}
It first tries to connect to an existing debugger prozess (needed for docker) and opens a new one if not found (needed for Homey19 or Homey23 remote).
In lauch.json you can provide different profiles for Homey remote or docker connection or different settings for Homey19/23:
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach HA to Homey Dev",
"address": "192.168.1.13",
"port": 9225,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
},
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach HA to Homey Pro 23 Wifi",
"address": "192.168.1.15",
"port": 9225,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
},
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach HA to Homey23 Docker",
"address": "192.168.1.15",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
}
Another hint, it’s also possible to script homey select
to switch between Homeys!
{
"version": "0.2.0",
"configurations": [
{
"name": "Select Homey Jr.",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "homey",
"args": ["select", "-n", "Homey Jr."]
},
{
"name": "Select Homey Pro Early 2023",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "homey",
"args": ["select", "-n", "Homey Pro van Danee"]
},
{
"name": "Launch app on Homey Jr.",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "homey",
"args": ["app", "run"],
"outputCapture": "std",
"serverReadyAction": {
"pattern": "Debugger listening on",
"action": "startDebugging",
"name": "Attach to Homey"
}
},
{
"name": "Attach to Homey Jr.",
"type": "node",
"request": "attach",
"address": "192.168.1.98",
"port": 9222,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
},
{
"name": "Launch app on Homey Pro Early 2023",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "homey",
"args": ["app", "run", "--remote"],
"outputCapture": "std",
"serverReadyAction": {
"pattern": "Debugger listening on",
"action": "startDebugging",
"name": "Attach to Homey"
}
},
{
"name": "Attach to Homey Pro Early 2023",
"type": "node",
"request": "attach",
"address": "192.168.1.191",
"port": 9222,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
}
]
}
Hey @DaneedeKruyff ,
When i use this launch.json, or any “Launch app” configuration, the app starts fine and std is both logged.
However, Breakpoints will not be set:
But, when i run the app from the build-in terminal and then attach the Debugger, this works fine.
However, i would like to run it with your Launch.json, because somehow, when launch like that, i do not have homey-api issues.
So, how do you use breakpoints?
I’ve done nothing special to use break points, I just click in the bar left of the line numbers to set one:
And then just run the Launch App on Early?
Well, i guess not:
When i run the launch, and thus remote, i guess i needed a separate debugger and listener.