node.js debugging - how to set a breakpoint

Oscar Brito

A lot of people say that the state of node.js tooling is not as advanced as in other languages, like Java or C#. Believe it or not but javascript debug tools have been around since the rise of the world wide web and thanks to Mozilla and Google they are pretty cool nowadays.

I'm trying so hard right now not to write about the fallacies that people use when they compare other languages to javascript but I'll save that to another post.

node.js debug with node-inspector

node debugger

Like every major programming platform, node.js comes with a built-in debugger that can be executed by running node debug app.js.

The application will start paused and you'll need to use the debugger commands to run the app. By pressing c the application will continue it's execution.

The debugger is a client application that connects to the port 5858 that the node interpreter opens when in debug mode.

Pro tips

  1. Use the debugger; statement on your code to create auto breakpoints, pausing the application.

  2. If you have a node.js app already running run kill -SIGUSR1 $PID to enable debug mode on it.

I want to use an IDE!

**Usually people that are used to strong typed languages don't like weak typed languages, like javascript, and one of the reasons is because there's no strong need for an IDE.

If you take the IDE away from a java developer they feel really insecure.

Once you have your node.js app running in debug mode you can use free node.js debugger clients like node-inpector and iron-node, or use proprietary IDE like JetBrains WebStorm, or Visual Studio Code.

node-inspector

I personally like free and open source debugging tools, so I'll describe how debug a node.js application using node-inspector.

  1. node debug app.js

  2. press c to continue the application execution

  3. node-inspector and open http://127.0.0.1:8080/debug?port=5858 to debug the app using a chrome developer tools alike debugger.

Conclusion

Node.js and javascript are considered by many just a hype that will never be used on enterprise applications.

If you ask me, the tooling around javascript is powerful and provides all that you need to debug all aspects of an application.

Again, I'm not trying to say node.js and javascript are better than X, but making the point that the tooling is there and available for you to use.

Oscar out.