I have been working on a project called Wackcoon (more on that later) that uses Node and runs on a Raspberry Pi. Like all development projects, I needed to debug not only my work but sometimes errors being thrown from node modules that I am using. Wrapping up my code in
Try/Catch blocks and sending the error to
console.log() got old really fast. Since I could not test locally since I am running Linux on the Raspberry Pi, I wanted to use Visual Studio Code to remote debug my project on the Raspberry Pi device itself.
The Raspberry Pi is on my network at home, so on the Pi, I ran an
ifconfig command to get the IP address of the device.
Then I went to Visual Studio Code and clicked on the debug icon (little bug in the figure below) I also made sure that
Attach was selected in the debug dropdown on top.
Next, I modified my launch.json file. This is created the first time you run debug (the green arrow). In the launch.json file there are two sections. One configuration is for launch (debugging locally) and the other is for attaching to an already-running process. I put in the IP address that I got in the last step.
Now on the Raspberry Pi, I open a terminal and run the app using the
–debug-brk option. I can also just use the
–debug option but the first one will have the application pause and wait for me on the first line of my code.
Now when I start my debug in Visual Studio Code (green arrow) you can see that it is sitting in break mode at line one.
I can set breakpoints, step in, step over, step out, add watches, etc. Since this is being streamed from the Pi, I cannot modify it on the fly like you can when you debug locally but still pretty cool.
Hope this helps.