NodeJs was released almost 9 years ago. The default debugging process of NodeJs (read Node.js) is quite clumsy. You are already aware of the need to add --inspect to the node script with node inspector. It is also dependent on chrome. Then look at the proper web socket connection which is hard and debug using chrome node debugger. To be honest, it is a pain in the neck.
I am going to show a step by step process of debugging nodejs application with ndb. In other words, you will learn how to debug a nodejs application using ndb. Below you can see ndb in action, let’s roll up your sleeves and get started:
Below are some prerequisites before you get started:
You have nodejs installed on your system (a no-brainer but still worth a mention)
You have general knowledge of running node scripts and working with nodejs apps.
You have prior debugging experience with nodejs or any other language.
For debugging nodejs application in place of just another script I will use a full nodejs express application. It is an open source application I used for a demo on testing nodejs application.
I am using my opensource currency API for this step-by-step getting started of debugging a nodejs application. It is built using express js framework. You can also check the running app hosted on Zeit Now to see the USD to AUD rate of 2019–01–10 as an example.
The idea of the application is simple. If the conversion rate is available in the database it will fetch it from the database. If not it will fetch it from another API and send it back to the user also saving the rate in the database at the same time (async) for later use.
You can clone the application from github and run npm install to get it ready for debugging. This is a very simple application with most of the logic in exchangeRates.js file. It has mocha tests too as it was a demo for testing nodejs application.
Installing ndb is very easy. All you need to do to get started with debugging your nodejs application is to install ndb. I would suggest to install it globally with:
# with npm npm install -g ndb #with yarn yarn global add ndb
You can also install and use it locally per app if you want. One thing I had to fix was to get the latest version of chrome as I saw some permission issues.
For debugging nodejs applications with ndb you can directly run the nodejs app script with ndb rather than node. For example, if you were used to doing node index.js or nodemon index.js in development. To debug your app you can run:
Notice that you don’t need to put any -- inspect so the experience is a lot smoother.
ndb opens up a screen like below when you do ndb . or ndb index.js:
Please add a breakpoint on line 46. As you have run the application with ndb it will run in debug mode and stop at the breakpoint like below when you hit http://localhost:8080/api/convert/USD/AUD/2019-01-01 on the browser. I have set the breakpoint on exchangeRates.js like 46 in the screenshot below:
ndb allows you to run any script for debugging. For example, I can run ndb npm start and it will use the nodemon run. This means I can debug the application while changing the code which is great.
You can also debug your test with a command like ndb npm test.
As the debugger is working I can place more break points or run through the code at my speed and convenience.
The usual debugging workflow I assume you are familiar with. below I have advanced to line 52:
As any other debugger with ndb you can:
Check the call stack trace
Check the process
Read more about what you can do with ndb in the official readme. Below is a screenshot of the useful console:
Debugging any nodejs application with ndb is a better developer experience. To debug the currency API nodejs express app with ndb you run do the following commands give you have node > 8 installed:
npm install -g ndb
git clone [email protected]:geshan/currency-api.git
ndb npm start
After the ndb debugger opens up add a breakpoint at line 46 of src/exchangeRates.js
Then open http://localhost:8080/api/convert/USD/AUD/2019-01-01 in the browser
Now as the app should pause at the breakpoint, Enjoy! and continue debugging.
If it works for this app, you can debug any of your nodejs application with this approach.
I hope this post has helped you debug your nodejs application better. If you have any other things to share about debugging nodejs apps or better usage of ndb please comment below!
Originally published at geshan.com.np.