Debugging a nodejs cli script with --inspect

Tags: NodeJS, Bash
 

NodeJS scripts from the CLI can be difficult to debug. Often, one doesn't want to modify the core processes.

I found the easiest way to hit a breakpoint was to create a shell wrapper.

But I was worried... would my code have access to sdin and stdout?

The answer is yes, scripts called from within scripts have access to sdin and stdout.

Call your script from within a shell script.

Here's the wrapper script.

#!/bin/bash
 
exec node  --inspect-brk=9229 ./myscript.js

Here are some notes:

  • In this case, I am debugging on port 9229. You can use whatever port you like.
  • You need to use --inspect-brk and not just plain --inspect for Chrome and PHPStorm scripts.
  • For Chrome, follow these instructions.
  • For PHPStorm read this and then do this:
  • 300_node inspect settings.png
    300_debug button.png

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.