So let's start with the simplest way to debug with the console object.
Save the script and run it, the console will output in each iteration the value of the variables used in this case "i" and "sum", like the following:
Iteration:0, sum:0
Iteration:1, sum:1
Iteration:2, sum:3
Iteration:3, sum:6
Iteration:4, sum:10
Iteration:5, sum:15
Iteration:6, sum:21
Iteration:7, sum:28
Iteration:8, sum:36
Iteration:9, sum:45
The console object has other methods besides log(), you can find more about it in the following link:
http://nodejs.org/api/stdio.html
Since V8 node comes with a debugger, it has a built in client for this debugger to use it just add the debug argument when start running the script. Save the previous script as scriptDebug.js then type the following command to start the debugger:
node debug scriptDebug.js
After that you will see a prompt to the debugger client as the following:
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug>
To set a break point you could use the command sb(), for example to set a break point in the line 5, it would be like this:
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> sb(5);
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
4 sum = sum + i;
* 5 console.log('Iteration:' + i +', sum:' + sum);
6 }
Once you can go through your code, what is missing is to inspect the values of the variables in your program, to do this let's use the watch command in the script we have used so far, the following example show how to use it:
$ node debug scriptDebug.js
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> watch('sum')
debug> cont
< Iteration:0, sum:0
break in scriptDebug.js:6
Watchers:
0: sum = 0
4 sum = sum + i;
5 console.log('Iteration:' + i +', sum:' + sum);
6 debugger;
7 }
8
debug> cont
break in scriptDebug.js:6
< Iteration:1, sum:1
Watchers:
0: sum = 1
4 sum = sum + i;
5 console.log('Iteration:' + i +', sum:' + sum);
6 debugger;
7 }
8
As you can see now every time a breakpoint is found the variable sum is printed with its value.
With this tool you can easily go through your code, stop wherever you want and do many more things, you can find all the instructions in the following link:
http://nodejs.org/api/debugger.html
The debbuger that comes with node.js is very powerful but still is a command based utility, there are debbugers with more visual help, lets use node inspector to have a visual aid to debug our script.
First lets install globally node inspector, with the help of npm use the following command:
$ npm install -g node-inspector
With node inspector installed let's start it with the following command:
$ node-inspector
Node Inspector v0.7.3
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
With node inspector running in other terminal you have to start in debug mode your application with the following command:
$ node --debug-brk scriptDebug.js
debugger listening on port 5858
The --debug-brk option is a node inspector option which starts to debug the script and stops it in the first line.
Now let's open a browser with url indicated "http://127.0.0.1:8080/debug?port=5858" by node inspector, you will see the following console:
As you can see the script is displayed and in the right side there is a window that displays all the information about the runtime values of the script, as in the upper right side there are some navigation buttons to go through the script.
With node inspector the debugging of a script is made so much easier because it is a visual tool and all the commands are a click away.
You can find more about node inspector in the following link:
https://github.com/node-inspector/node-inspector
This is a brief example of how to start using the debug tools of node.js and I hope it is useful to you.
Iteration:0, sum:0
Iteration:1, sum:1
Iteration:2, sum:3
Iteration:3, sum:6
Iteration:4, sum:10
Iteration:5, sum:15
Iteration:6, sum:21
Iteration:7, sum:28
Iteration:8, sum:36
Iteration:9, sum:45
The console object has other methods besides log(), you can find more about it in the following link:
http://nodejs.org/api/stdio.html
Since V8 node comes with a debugger, it has a built in client for this debugger to use it just add the debug argument when start running the script. Save the previous script as scriptDebug.js then type the following command to start the debugger:
node debug scriptDebug.js
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug>
Now the debug client support some basic commands to debug the script, here are some of them:
- cont, c - Continue execution
- next, n - Step next
- step, s - Step in
- out, o - Step out
- pause - Pause running code (like pause button in Developer Tools)
- setBreakpoint(), sb() - Set breakpoint on current line
- clearBreakpoint, cb(...) - Clear breakpoint
- .....
In the debugg mode if you type the command cont as this:
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> cont
< Iteration:0, sum:0
< Iteration:1, sum:1
< Iteration:2, sum:3
< Iteration:3, sum:6
< Iteration:4, sum:10
< Iteration:5, sum:15
< Iteration:6, sum:21
< Iteration:7, sum:28
< Iteration:8, sum:36
< Iteration:9, sum:45
program terminated
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> cont
< Iteration:0, sum:0
< Iteration:1, sum:1
< Iteration:2, sum:3
< Iteration:3, sum:6
< Iteration:4, sum:10
< Iteration:5, sum:15
< Iteration:6, sum:21
< Iteration:7, sum:28
< Iteration:8, sum:36
< Iteration:9, sum:45
program terminated
You will see will be the output of the program, this is because the is no breakpoint in the code.
To set a break point you could use the command sb(), for example to set a break point in the line 5, it would be like this:
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> sb(5);
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
4 sum = sum + i;
* 5 console.log('Iteration:' + i +', sum:' + sum);
6 }
Now every time a continue command is given it will stop in the breakpoint, as the following examples shows:
debug> cont
< Iteration:0, sum:0
break in scriptDebug.js:5
3 for(i=0;i<10;i++) {
4 sum = sum + i;
* 5 console.log('Iteration:' + i +', sum:' + sum);
6 }
Another way to place a breakpoint is to set the debugger instruction in your code as the following example:
Once you can go through your code, what is missing is to inspect the values of the variables in your program, to do this let's use the watch command in the script we have used so far, the following example show how to use it:
$ node debug scriptDebug.js
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
1 var sum = 0;
2
3 for(i=0;i<10;i++) {
debug> watch('sum')
debug> cont
< Iteration:0, sum:0
break in scriptDebug.js:6
Watchers:
0: sum = 0
4 sum = sum + i;
5 console.log('Iteration:' + i +', sum:' + sum);
6 debugger;
7 }
8
debug> cont
break in scriptDebug.js:6
< Iteration:1, sum:1
Watchers:
0: sum = 1
4 sum = sum + i;
5 console.log('Iteration:' + i +', sum:' + sum);
6 debugger;
7 }
8
With this tool you can easily go through your code, stop wherever you want and do many more things, you can find all the instructions in the following link:
http://nodejs.org/api/debugger.html
The debbuger that comes with node.js is very powerful but still is a command based utility, there are debbugers with more visual help, lets use node inspector to have a visual aid to debug our script.
First lets install globally node inspector, with the help of npm use the following command:
$ npm install -g node-inspector
With node inspector installed let's start it with the following command:
$ node-inspector
Node Inspector v0.7.3
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
$ node --debug-brk scriptDebug.js
Now let's open a browser with url indicated "http://127.0.0.1:8080/debug?port=5858" by node inspector, you will see the following console:
As you can see the script is displayed and in the right side there is a window that displays all the information about the runtime values of the script, as in the upper right side there are some navigation buttons to go through the script.
With node inspector the debugging of a script is made so much easier because it is a visual tool and all the commands are a click away.
You can find more about node inspector in the following link:
https://github.com/node-inspector/node-inspector
This is a brief example of how to start using the debug tools of node.js and I hope it is useful to you.
Thanks for this value able post. I have read all the things very carefully its really a helpful and effective post.
ReplyDeleteHire Nodejs Developers
Useful & Great Article for NodeJS Development
ReplyDeleteNode.js Training
Thank you so much for sharing this information. I appreciate your efforts on making this collection.
ReplyDeleteNode.JS Development
Great article.Tutorial is very nice..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.Keep updating.Have a look on nodejs development company for more.
ReplyDeleteYour article is really worth reading. I've been following your blogs for a while and you are doing a great job. Regards to your hard work and great effort.
ReplyDeleteOracle Training in Chennai
Oracle Course in chennai
Tally Course in Chennai
Tally Classes in Chennai
Embedded System Course Chennai
Embedded Training in Chennai
Oracle Training in OMR
Oracle Training in Porur
Thanks for sharing this amazing post.
ReplyDeleteSEO Services in Chennai
Best SEM Services in Chennai
I am really happy with your blog because your article is very unique and powerful for new.
ReplyDeleteDimension Control Services in Birmingham
plant engineering services in Hovedstaden Denmark
3D Laser Modelling
3d Laser Scanning Services in Glasgow
Point cloud to 3D Model Hovedstaden, Denmark
Scan to BIM in Georgia
A web design agency in Dallas is a professional service provider that specializes in creating visually appealing and functional websites for businesses and individuals in the Dallas area. These agencies have a team of skilled designers, developers, and digital strategists who collaborate to deliver customized web solutions that meet the unique needs and objectives of their clients. From crafting eye-catching layouts to incorporating user-friendly features and optimizing for search engines, the web design agency in Dallas aims to enhance the online presence and user experience of businesses, helping them establish a strong digital footprint and achieve their goals in the competitive online landscape.
ReplyDelete