Debugging NodeJS Application
This repository contains guide for how to do debug into NodeJS Application.
If we are working on any NodeJS project/application, then it doesn't matter, we are starter or expert, or project is already live or building from scratch, Debugging is most important thing which helps us a lot to do development.
Note: We are assuming NodeJS is installed in your system, and you have basic knowledge of JavaScript and NodeJS.
As we all know, NodeJS runs on Google's V8 engine and V8 has extensive debugger. NodeJS has also provided built-in client for this debugger.
To debug any NodeJS project/application we have to follow 3 simple below steps:
- Write
debugger;
into that part of program/project which we want to debug. Thatdebugger;
will work as breakpoint. - To run our NodeJS application into debug mode, type
node debug
command and this command must be followed by our main JavaScript/.js file. For this blog we are using app.js as main JavaScript file, so while running the app.js file into debug mode, we have to type/run below command into location where app.js(main) JavaScript file is located:node debug app.js
- To play with debugger, we need to know about below 4 operators:
- cont: It is used to continue/move to next breakpoint/debugger of the program.
- next: It is used to move to next statement/line of the code/program.
- repl: It is used to evaluate the code.
- quit: It is used to quit from the debugging the program.
- help: It is used to see other commands.
Enough for theory part. Let's debug a simple program:
Open your favourite editor, and copy paste below code in that, then save it as app.js.
/**
* Created by Amit Thakkar on 6/11/14.
*/
console.log("Program starting...");
var one = 1, two = 2;
var user = {
name: "Amit Thakkar",
age: 20
};
user.age += one++ + ++two + ++one;
debugger;
user.age -= one-- - --two - --one;
debugger;
console.log("My Name is", user.name, "and Age is", user.age);
For debugging app.js
lets follow all above 3 steps:
-
Add
debugger;
into the code: After looking at theapp.js
code, we know that we have placed only 2debugger
/breakpoint
into the above code, that is on line no. 11 and 13. -
Run
NodeJS
indebug
mode: Type and runnode debug app.js
. -
Play with
debugger
: Lets use cont, next, repl and quit as below: