ndb
is a debugger for Node.js applications that is built on top of the built-in debug
module. It provides a more intuitive interface and additional features such as automatic source map support and inline evaluation of expressions.
To use ndb
, you first need to install it globally using npm:
npm install -g ndb
Then, you can start debugging your Node.js application by running the ndb
command followed by the name of your script:
ndb your-app.js
This will start the debugger and open a new tab in your default web browser with the debugger interface. You can then set breakpoints, step through your code, and inspect variables as you would with any other debugger.
Here is an example of using ndb
to debug a simple Node.js script that calculates the factorial of a number:
// factorial.js
function factorial(n) {
if (n === 1) return 1;
return n * factorial(n - 1);
}
const result = factorial(5);
console.log(result); // 120
To debug this script using ndb
, you would run the following command:
ndb factorial.js
This would start the debugger and open a new tab in your web browser. You can then set a breakpoint on the if
statement by clicking on the line number in the debugger interface. When you run the script again, the debugger will stop at the breakpoint and allow you to step through the code and inspect variables.
You can also evaluate expressions in the debugger interface by typing them into the console at the bottom of the screen. For example, you could evaluate n
to see its value at any point in the execution of the script.
> n
5
Overall, ndb
is a powerful tool for debugging Node.js applications that provides an easy-to-use interface and additional features such as source map support and inline expression evaluation.