node.exe debugging JavaScript codeObjective:
Console.log can print some information, the light has log is not enough, when the problem of the program through the log can be located in the wrong location, but when we want to see the wrong site variables, log is powerless, in general, we do not print all the variables. At this point, you need the function of the breakpoint, in the program inside the breakpoint, directly to the wrong location, analysis error site to confirm the cause of the error.
Three modes:
The Nodejs internally provides a debug mechanism that allows the program to enter debug mode, allowing the developer to analyze code discovery issues Step-by-step.
A total of 3 in the start parameter can let the program into debug mode
Node Debug app.js
Node--debug app.js
Node--debug-brk app.js
There are some differences between the 3 modes in the debugging form.
1.node Debug App.js
1). This way to start the program, the program will enter the debug mode, and run to the start of the file on the 1th line to stop, waiting for the developer to go down the command
2). Start the program in this way and enter debug mode directly in the current CMD
2.node--debug app.js
1). Starting the program this way, the program goes into debug mode and runs through all the code. This type of startup is often used during program startup without debugging, by triggering time into the callback function, such as a breakpoint in an HTTP request, waiting for the client to enter the breakpoint after access
2). This way to start the program, will open a TCP port monitoring, in this CMD does not enter the debug mode, you need to open the terminal with the node debug command to connect the debug port
Command for node debug localhost debug port
or node debug p node process ID
3.node--debug-brk app.js
1). Starting the program this way, the program goes into debug mode, but does not run the code until a terminal is connected to the debug port before the code is executed and enters the breakpoint on line 1th
2). This way to start the program, will open a TCP port monitoring, in this CMD does not enter the debug mode, you need to open the terminal with the node debug command to connect the debug port
After entering debug mode, you can set breakpoints, cancel breakpoints, and control program execution flow with some commands.
Common commands:1. Process Control related
- 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)
2. Breakpoint Settings Cancellation related
- Setbreakpoint (), SB ()-Set breakpoint on current line
- Setbreakpoint (line), SB (line)-Set breakpoint on specific line
- Setbreakpoint (' fn () '), SB (...)-Set breakpoint on a first statement in functions body
- Setbreakpoint (' script.js ', 1), SB (...)-Set breakpoint on first line of Script.js
- Clearbreakpoint (' script.js ', 1), CB (...)-Clear breakpoint in script.js on line 1
3. Variable View related
- BackTrace, Bt-print backtrace of the current execution frame
- List (5)-list scripts source code with 5 line context (5 lines before and after)
- Watch (expr)-ADD expression to watch list
- Unwatch (expr)-Remove expression from watch list
- Watchers-list all watchers and their values (automatically listed on each breakpoint)
- Repl-open Debugger ' s repl for evaluation in debugging script ' s context
Variable names can be entered in REPL mode to view variable contents
Example:1. Node Debug
Enter the breakpoint from the first line of code and command N to enter the next line
2. Node--debug
CMD1 opening the Debug port
CMD2 Connecting the Debug port
Set breakpoints, cancel breakpoints
Cmd1 a minute to print.
Connect the Debug module in the same way as the process ID
You can see that the PID is 4436
REPL mode
References:
Http://www.cnblogs.com/tzyy/p/5028348.html
Http://www.cnblogs.com/dolphinX/archive/2013/12/22/3485345.html
"Go" node.exe debugging JavaScript code