These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
As you probably have already experienced, when your code breaks, it can be a pain to debug even for the most logical of minds:
This lesson covers the Node.js built-in debugger. By the end of this lesson, you will be equipped with the basic skills needed to troubleshoot your code when it breaks or is not working how you expect.
A debugger or debugging tool is a computer program that is used to test and debug other programs (the "target" program).
A debugger allows you to manipulate the execution of your code such that you can examine its state at a particular point in time or over the course of various points in time. Imagine being able to pause execution and step line by line through your code. Now imagine that at any given point in your code, you can see the value of variables in the current scope. That is the power and usefulness of a debugger!
Getting started with the Node.js debugger is as simple as typing the word debugger
. This is actually a keyword in Node.js and fires up the built-in debugger. More specifically, it sets a breakpoint at the line you type debugger
.
Here is an example:
var foo = 'bar';
var left = 1;
var right = 2;
function sum(a, b) {
var c = a + b;
debugger; // <<<< Stops execution
return c;
}
console.log(foo);
console.log(sum(left, right));
Once you're ready to debug, you can enter debug mode with a slight alteration to the node
command:
$ node debug filename.js
After running the node
command with the debug
subcommand you'll enter the interactive Node debugger. This will by default stop at the first line of code in the file. At this point, you have some options as to commands that you can enter to guide execution. The most important of these are:
n
or next
: Step to the next statementc
or cont
: Continue to the next debugger;
statementThis means that we can step line by line with the n
command and if we want we can jump to the debugger;
line in the sum
function with the c
command. But you'll notice that if you attempt to access variables by name at this point, it results in an error:
break in debugging.js:3
1
2 var foo = 'bar';
> 3 var left = 1;
4 var right = 2;
5
debug> foo
ReferenceError: foo is not defined
at repl:1:1
at ContextifyScript.Script.runInContext (vm.js:37:29)
at Object.exports.runInContext (vm.js:69:17)
at Interface.controlEval (_debugger.js:967:21)
at REPLServer.eval (_debugger.js:741:41)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:544:10)
at emitOne (events.js:96:13)
at REPLServer.emit (events.js:188:7)
debug>
This is because, in order to access variables, you must first enter the debugger REPL.
repl
Once you're debugging, you're going to want to inspect the values of your variables to see what is causing problems. You can do this by entering the repl
command:
break in debugging.js:3
1
2 var foo = 'bar';
> 3 var left = 1;
4 var right = 2;
5
debug> repl
Press Ctrl + C to leave debug repl
> foo
'bar'
>
After you've entered the repl
command, you can type the name of the variable that you want to inspect. The value will then be printed on the next line like you'd expect in the Node.js REPL.
Here are the important snippets of code from this lesson.
var foo = 'bar';
var left = 1;
var right = 2;
function sum(a, b) {
var c = a + b;
debugger; // <<<< Stops execution
return c;
}
console.log(foo);
console.log(sum(left, right));
$ node debug filename.js
break in debugging.js:3
1
2 var foo = 'bar';
> 3 var left = 1;
4 var right = 2;
5
debug> repl
Press Ctrl + C to leave debug repl
> foo
'bar'
>
Now that you've learned how to use the Node.js debugger, you can easily inspect and troubleshoot your own code. Debugging is an important skill to develop and you will be constantly improving your workflow and seeking out new tools to aid you in this process.