Debugging Node Code

Debugging Node Code
0

#1

So I’ve come across a strange “wall” with my Node.js/MEAN stack journey. This wall is debugging.

I come from a C#/.NET background where when using Visual Studio Community 2015, I can simply set a breakpoint in the code, such as on a controller which takes input from a route (Equivalent of app.get or app.post in Express), and then the debugger will listen for when I access that route via a web browser, and then it will allow me to step through the code as soon as I access the route via the web browser, allowing me to see all of the passed variables and outcome of the code.

However, I am having a beast of a time in getting this to work with Node.js. In addition, I have a community/help group of MEAN developers in my area through the local FCC meetup, all who seem to be completely oblivious to debugging. So this raises several questions:

  1. Is using the debugger “not a thing” yet in Node? If so, why not and what are alternative ways to debug my node code?

  2. If node code can be easily debugged, how?

  3. Debugging is one of the absolute most core components of software engineering and it is how I solve 99% of my software issues. Without it, I feel lost at times and at a severe handicap.

What I have tried:

I usually use Visual Studio Code with Node.js which is a fantastic editor. I does have a debug feature, but it behaves significantly differently than the Visual Studio C#/ASP.NET debugging that I am used to.

For example, in ASP.NET using Visual Studio, if I am having an issue with controller logic, I set a breakpoint on the controller’s method and then hit F5 for Debug. I then launch a browser and when I hit the controller’s endpoint/route, the code execution stops at the breakpoint and allows me to step through code.

However, this doesn’t appear to be the case with the Visual Studio Code/Node debugger. In Node, when I set a breakpoint and then run the program, it does stop at the breakpoint, but it does it before I even browse to the controller, which makes it fundamentally useless because without my browser passing any data to the controller, the debugging session is faulty and of no help. Also, debugging seems to be strangely absent even from paid tutorials I’ve purchased online for Node.

Anyone have any advice for me? I’m finding this quite odd. I’ve heard “Well, JS is an interpreted language and C# is compiled” which is true, but Im not buying that as a valid excuse to have such a complicated/useless debugging system because clearly the debugger has the capability of proper functionality as indicated by the fact that it indeed does allow full stepthrough and watches.

Last but not least. I am aware of alternatives such as console.log and “manual” debugging, however, I am speaking of the actual debugger here. Thank you.


#2

https://nodejs.org/en/blog/release/v6.9.0/

The LTS upgrade of Node includes a debugger flag. I haven’t used it yet, but it may be worth investigating.


#3

I love well tested code.

I suspect your troubles are caused by setting breakpoint in the wrong place.

In this sample express app:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

When you set breakpoint on line:
app.get('/', function (req, res) {
It will stop immediately when you run the program. But if you set the breakpoint inside of a callback, on the following line:
res.send('Hello World!');
the debugger stops when the browser tries to access the endpoint.

(app.get is called in a different thread than its callback, so it’s not like synchronous code in C#)


#4

@marzelin So what are you using for your debugger?


#5

I’ve migrated from JavaScript to its superset: TypeScript. I use Visual Studio Code. It has debugger built-in and awesome intellisense feature. It’s so much better experience than writing plain JavaScript.


#6

You’re a hero. The JS community needs more people like you.

I’ve asked about this to like 25-30 people. A Google Engineer, Stack Overflow.

You’re the only one who’s been able to solve the issue.

@marzelin what type of work have you done? I want to see it!


#7

I’m currently working on Image Search Abstraction Layer using Test Driven approach.
Here’s a sneak peek of how I do it.


#8

Nice! I haven’t seen Gitbook before; it looks like an awesome platform for long form tutorials based on building whole projects.


#9

No code yet though huh?


#10

Click the green ‘Read’ button and it’ll let you navigate through the ‘book’. It took me a couple of goes to notice it.

There’s a fair bit of content already