# Debug app code

**URL:** <https://community.homey.app/t/debug-app-code/33516>\
**Category:** Developers\
**Created:** [August 12, 2020, 8:59am UTC](https://community.homey.app/t/debug-app-code/33516 "2020-08-12T08:59:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![avd88](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/avd88/32/17548_2.png) [@avd88](https://community.homey.app/u/avd88)\
**Post date:** [August 12, 2020, 8:59am UTC](https://community.homey.app/t/debug-app-code/33516/1 "2020-08-12T08:59:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 12, 2020, 10:42am UTC](https://community.homey.app/t/debug-app-code/33516/2 "2020-08-12T10:42:06Z")

</div>

> [@avd88](#):
>
> 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`:

```auto
require('inspector').open(9229, '0.0.0.0')

```

That will start the [V8 inspector](https://nodejs.org/api/inspector.html) 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.

---

<div class="post-metadata">

**Author:** ![ChrisG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/chrisg/32/8825_2.png) [@ChrisG](https://community.homey.app/u/ChrisG)\
**Post date:** [August 13, 2020, 6:21am UTC](https://community.homey.app/t/debug-app-code/33516/3 "2020-08-13T06:21:10Z")

</div>

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

[code:](https://github.com/cgHome/org.cflat-inc.dingz/blob/master/app.js#L3)

// 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);  
}

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [August 13, 2020, 8:27pm UTC](https://community.homey.app/t/debug-app-code/33516/4 "2020-08-13T20:27:04Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/f/2f05c5803a948c876f43b4b3e8a43171de179e2c.png)

---

<div class="post-metadata">

**Author:** ![ChrisG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/chrisg/32/8825_2.png) [@ChrisG](https://community.homey.app/u/ChrisG)\
**Post date:** [August 14, 2020, 4:59am UTC](https://community.homey.app/t/debug-app-code/33516/5 "2020-08-14T04:59:52Z")

</div>

launch.json settings

```
	    {
			"type": "node",
			"request": "attach",
			"name": "Attach to Homey",
			"address": "192.xx.xx.xx", <- homey ip-address
			"port": 9229,
			"localRoot": "${workspaceFolder}",
			"remoteRoot": "/"
		}
```

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [August 14, 2020, 6:00am UTC](https://community.homey.app/t/debug-app-code/33516/6 "2020-08-14T06:00:36Z")

</div>

Perfect, thank you.  
Just one small note about

> [@ChrisG](#):
>
> / 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);  
> }

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

---

<div class="post-metadata">

**Author:** ![ChrisG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/chrisg/32/8825_2.png) [@ChrisG](https://community.homey.app/u/ChrisG)\
**Post date:** [August 14, 2020, 6:28am UTC](https://community.homey.app/t/debug-app-code/33516/7 "2020-08-14T06:28:00Z")

</div>

> [@Adrian\_Rockall](#):
>
> So for anyone that wants to use it, replace all the “ with ’

Never mind, I use " 😜

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 14, 2020, 6:46am UTC](https://community.homey.app/t/debug-app-code/33516/8 "2020-08-14T06:46:45Z")

</div>

> [@Adrian\_Rockall](#):
>
> So for anyone that wants to use it, replace all the “ with ’

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:

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/c/8/c818b0b3befa859fd3f05600504ab5788e40ebfb.png)

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [August 14, 2020, 7:10am UTC](https://community.homey.app/t/debug-app-code/33516/9 "2020-08-14T07:10:53Z")

</div>

> [@Adrian\_Rockall](#):
>
> 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 ’

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.

---

<div class="post-metadata">

**Author:** ![avd88](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/avd88/32/17548_2.png) [@avd88](https://community.homey.app/u/avd88)\
**Post date:** [August 14, 2020, 8:24am UTC](https://community.homey.app/t/debug-app-code/33516/10 "2020-08-14T08:24:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Robin\_Oberst](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robin_oberst/32/32263_2.png) [@Robin\_Oberst](https://community.homey.app/u/Robin_Oberst)\
**Post date:** [January 7, 2022, 3:12pm UTC](https://community.homey.app/t/debug-app-code/33516/11 "2022-01-07T15:12:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [January 8, 2022, 6:11am UTC](https://community.homey.app/t/debug-app-code/33516/12 "2022-01-08T06:11:43Z")

</div>

It should be ‘homey app run’. Simple typo by op.

---

<div class="post-metadata">

**Author:** ![Joolee](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/joolee/32/3064_2.png) [@Joolee](https://community.homey.app/u/Joolee)\
**Post date:** [February 9, 2022, 10:50am UTC](https://community.homey.app/t/debug-app-code/33516/13 "2022-02-09T10:50:47Z")

</div>

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:

> <https://github.com/Joolee/nl.joolee.homey.espeasy/blob/master/.vscode/launch.json>

> <https://github.com/Joolee/nl.joolee.homey.espeasy/blob/master/app.js#L9>

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](https://github.com/microsoft/vscode/issues/9093)) shows a drop-down menu. Then you can switch in the debugger tab to the ‘attach’ tab.

---

<div class="post-metadata">

**Author:** ![Gruijter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gruijter/32/1973_2.png) [@Gruijter](https://community.homey.app/u/Gruijter)\
**Post date:** [February 5, 2023, 12:14pm UTC](https://community.homey.app/t/debug-app-code/33516/15 "2023-02-05T12:14:13Z")

</div>

@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:

```auto
     "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

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [February 14, 2023, 8:18am UTC](https://community.homey.app/t/debug-app-code/33516/16 "2023-02-14T08:18:49Z")

</div>

One thing to note, if you use `homey app run --remote` on the Homey 2023 then change:  
`"remoteRoot": "/"`  
to  
`"remoteRoot": "/app/"`

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [February 14, 2023, 8:45am UTC](https://community.homey.app/t/debug-app-code/33516/17 "2023-02-14T08:45:51Z")

</div>

Another hint…if you want to debug on Homey and Docker, you can add this code to your app.js:

```auto
		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:

```auto
        {
            "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/"
        }

```

---

<div class="post-metadata">

**Author:** ![DaneedeKruyff](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/daneedekruyff/32/964_2.png) [@DaneedeKruyff](https://community.homey.app/u/DaneedeKruyff)\
**Post date:** [February 14, 2023, 7:18pm UTC](https://community.homey.app/t/debug-app-code/33516/18 "2023-02-14T19:18:53Z")

</div>

Another hint, it’s also possible to script `homey select` to switch between Homeys!

```auto
{
	"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/"
		}
	]
}

```

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/5/e542be4db1a998e3ee90bdff8b2cb35708fa67c3.png)

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [February 15, 2023, 8:18am UTC](https://community.homey.app/t/debug-app-code/33516/19 "2023-02-15T08:18:41Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/0/40bae4209eee7640f6066d0dcee5414b7a2cdb24.png)

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?

---

<div class="post-metadata">

**Author:** ![DaneedeKruyff](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/daneedekruyff/32/964_2.png) [@DaneedeKruyff](https://community.homey.app/u/DaneedeKruyff)\
**Post date:** [February 15, 2023, 1:16pm UTC](https://community.homey.app/t/debug-app-code/33516/20 "2023-02-15T13:16:45Z")

</div>

I’ve done nothing special to use break points, I just click in the bar left of the line numbers to set one:

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/f/bf43089d78df5705f10142a8fd5cbfc4dfdfbcd9.png)

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [February 15, 2023, 1:20pm UTC](https://community.homey.app/t/debug-app-code/33516/21 "2023-02-15T13:20:05Z")

</div>

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.

[Next page](https://community.homey.app/t/debug-app-code/33516.md?page=2)
