Debugging ( Debugging ) Node.js with the built-in debugger .

Debugging Node.js with the built-in debugger .

Debugging (Debugging) Node.js with the Scrubber built-in. See the web version of your application is running, but the command line version is giving error. What’s going wrong? It can be hard to find. If the application simply ended without printing anything. Your try / catch and event handlers did not identify any error and the typical approach by console.log () does not print anything. So the best thing to do is to use Node.js debugger to debug your code. Some even speak debug the code, debugging is a word derived from the English word debugging (or simply debug).

Google’s V8 engine, where the Node wheel, comes with an extensive debugger accessible via TCP and Node.js has a built-in client for this debugger. This is considered the debugger (debug client) Node. This article teaches you how to use the Node debugger to debug your script exposing how to put breakpoints (breakpoints), run the Node REPL in the middle of debugging, go by the code and analyzing debugger exits.





The basic

Google’s V8 engine has a built-in debugger and the Node has a client to access this debugger
The debugger can be started using running node with the argument debug
In your application you can set breakpoints using the reserved word debugger
You can add vigilant to track the value of expressions while debugging

Now let every stage of debug action.
Inserting some breakpoints

A breakpoint ( breakpoint ) is a place in your code where you want your application momentarily stop so you can analyze the state of your program at that code point. If you do not use a breakpoint you will have go through the step-by- step code to get where you want to debug.

So basically , your script you will need to put some breakpoints in nearby places where you think your system is behaving improperly . Follow depurar.js example below .

x = 5;
setTimeout(function () {
debugger; // Point of interruption
console.log(“world”);
}, 1000);
console.log(“Ola”);

The breakpoint is added in the code using the reserved word debugger. This is an enhancement of the V8 engine, not necessarily the Node.js.

You can set many breakpoints in your code if you need.
Starting the Debugger

To start the debugger , run your script using the argument debug node in our example : node debug depurar.js . You will be taken to the debugger debug REPL > and then you will be able to use the help command to see the list of available commands and start and debug your code. When you open your debugger starts paused on the first line and you need to use the cont command to run your code to be stopped when the debugger to find the word reserved debugger or you use the pause command.
$ node debug depurar.js
< debugger listening on port 5858
connecting… ok
break in /home/rafadev/git/purifer/depurar.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug> cont
< Ola
break in /home/rafadev/git/purifer/depurar.js:3
1 x = 5;
2 setTimeout(function () {
3 debugger;
4 console.log(“world”);
5 }, 1000);
debug> next
break in /home/rafadev/git/purifer/depurar.js:4
2 setTimeout(function () {
3 debugger;
4 console.log(“world”);
5 }, 1000);
6 console.log(“Ola”);
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/rafadev/git/purifer/depurar.js:5
3 debugger;
4 console.log(“world”);
5 }, 1000);
6 console.log(“Ola”);
7
debug> quit
The next command evaluates the next line of your code and the repl command allows you to evaluate the current state of your program remotely via the Node REPL . There are also other commands available , remembering that you can type help to remember the commands available .
Code Listing and generating a tracking

The first thing you will possibly do is steer . Where exactly we are in the code ? To do this use the list function. This function prints a formatted segment of code where you are. This function takes as a parameter a number saying how many lines of context are impessas . Example of use : list ( 2 ) will print where you are over the previous 2 lines and 2 rear .
debug> list(2)
3
4 setTimeout(function () {
5 debugger;
6 console.log(“world”);
7 }, 1000);
Here we can see the current line context that the program is, then we can know where the program is paused. The next command to be evaluated will be marked with the color green, in this example the debugger word is green.
Vigilantes (Watchers)

You can watch the value of an expression or a variable while debugging your code. In each breakpoint vigilantes will be evaluated in the current context and their values will be printed soon after the interruption source.

To start watching a phrase, insert watch ( “your_expression”). The watchers command prints the active vigilant. To stop watching an expression, enter unwatch ( “your_expression”).
Drive with next, step in and out

The next command, as has been said, continuing implementation of the program assessing the current line and pausing in the first term of the next line. If the line to be evaluated refers to a method of another module, or even the core, you can also debug the inner workings of this method, just use the step function that is shortening to “step into this one” that can be translated to “get this” which basically comes to debug the method internally.

When you are internally debugging the operation of this method, you can go using the next and step commands to track in detail what is happening “behind the scenes” debugging to the Node core itself, if you want. Already out command is for you to go back to the code that called that method and pause to the next expression.

Command List
Movement

cont , c – Continue implementation
next , n – Next step
step , s – Sign
out, the – Exit
Pause – Pause the execution of a code (like the button to pause the development tools )

Breakpoints

setBreakpoint ( ) , sb () – Set it a breakpoint in the current line
setBreakpoint (line), sb ( line) – Set it a breakpoint on a particular line
setBreakpoint ( ‘ fn ()’ ) , sb (…) – Set it a breakpoint in the first statement in the function body
setBreakpoint ( ‘ script.js ‘, 1 ) , sb (…) – set it a breakpoint in the first line of script.js
clearBreakpoint , cb (…) – Removing a point interruption.

was also you can insert a breakpoint in a file ( module) that has not loaded , for example using the set Breakpoint command ( ‘ mod.js ‘, 23 ) adds a breakpoint in mod.js module on line 23 .

$ node debug main.js
< debugger listening on port 5858
connecting to port 5858… ok
break in /home/rafadev/git/purifer/main.js:1
1 var mod = require(‘./mod.js’);
2 mod.hello();
3 mod.hello();
debug> setBreakpoint(‘mod.js’, 23)
Warning: script ‘mod.js’ was not loaded yet.
1 var mod = require(‘./mod.js’);
2 mod.hello();
3 mod.hello();
debug> c
break in /home/rafadev/git/purifer/mod.js:23
21
22 exports.hello = function() {
23 return ‘hello from module’;
24 };
25
debug>

Information

backtrace , bt – Prints a current execution frame Call Log
list ( 5 ) – List the script source code with 5 lines of context ( 5 lines before and after 5 )
watch ( expr) – Add an expression to the list under observation
unwatch ( expr) – Removes an expression of guarded list
watchers – Lists all monitored and their values ( automatic listed on each breakpoint )
repl – Opens the REPL of debug to review the script debugging context
Execution Control

run – Execute the script ( runs automatically when the debugger is started)
restart – Restarts a script
kill – Kills a script

several

scripts – List all loaded scripts
version – Print the V8 engine version

Advantages of use

The V8 debugger can be skills do and accessed either by starting Node with the flag ( flag ) of –debug command line as signaling an existing Node process with SIGUSR1 .

Once the process was put in debug mode in this way it can be connected by node debugger . Both connected by pid as the URI for the debugger. The syntax is :

node debug -p < pid > – Connect to the process via pid
node debug – connects to the process via URI as localhost: 5858




LEAVE A REPLY

Please enter your comment!
Please enter your name here