Debugging Node with Vim

Debugging node is hard. For a long time, I didn't really understand why anyone would want to use debugger versus using console.log. I finally took the plunge and started using debug with vim about 3 months ago, and it's been a great attribute ever since, especially when working with node.js services.

Here is a quick down and dirty installation process for setting up Vim's node debugger:

npm install -g vimdebug

Installs node-vim-inspector
node --debug-brk index.js

Starts node in debug mode on index.js

Opens up the inspector for the vimdebugger

Inside of vim:

Note: Vim actually already has netbeans built in, so using :nbs is the best way to connect to the REPL, and gain access to the debugger options.

Starts netbeans and connects to debugger in vim

Here are the commands in the documentation.

CTRL+c - continue
CTRL+i - step in
CTRL+o - step over
CTRL+n - step next
CTRL+u - move one stack frame up
CTRL+d - move one stack frame down
CTRL+p - set breakpoint at current location

Not very useful to someone starting off debugging right? Let's go over them:

  • CTRL+c - continue - Basically run the entire application. Use if you want to ignore all current breakpoints.
  • CTRL+i - step in - Step into the current function. E.G. if you have a require('./check/me.js);. Choosing step in will allow you to open up that file and step through the code line by line if needed.
  • CTRL+o - step over - Exits the context you're currently working in and backs you up to the higher remaining file.
  • CTRL+n - step next - Skip the next Context, or function, or include.
  • CTRL+u - move one stack frame up - Moves up without executing code. It can be even used on the first line of code to move to the end of the file quickly!
  • CTRL+d - move one stack frame down - This actually will not execute the code, this will just travel forward, and show you what is going to be executed. It is helpful for making changes in advance if you see something that could be broken.
  • CTRL+p - set breakpoint at current location - Places a breakpoint in the code on that line so you can stop the code at that point. Use this with CTRL+d & CTRL+u to be able to make your way through code pretty quickly!

Using node-vim-debug is a great tool because it allows you to hop in and out of node.js code as it happens. Need to debug that pain in the ass express.js server? Drop it into node-vim-debug, and see where you're erroring out at. It can save a lot of trouble where normally you would need to use console.log's in order to find out where your issues are at.

There is a lot of documentation already on how to properly use vim debugger, but the biggest thing to learn is to learn how the REPL works. Being able to type in an object, and seeing what it consists of is a great feature.

Show Comments

Get the latest posts delivered right to your inbox.