Nodejs Debugging Methods

Source: Internet
Author: User

After two days of tinkering with the node project, take a look at node's debugging methods.

The debugging of the front-end development can be debugged by logging (Console.log) or breakpoint debugging (debugger; with chrome or firebug) or even the IDE (Webstorm).

node is the same.

Hit the log

Console.dir,console.log, the introduction of asserts module, debug area for debugging and other methods, this method is usually detected or can be, there is a serious bug when there will be a violent poor lift feeling.

Breakpoint Debugging

There are several ways to debug breakpoints

A.node.js built-in Debugging tools

TCP-based protocol, NODEJS scripts can be debugged through the Commod interface

$ node Debug myscript.js< debugger listening on port 5858

Connecting Okbreak in/home/indutny/code/git/indutny/myscript.js:1

1 x = 5;

2 SetTimeout (() = {

3 debugger;

Debug>

Use debugger on the script, and set breakpoints.
Run the script in debug mode, node Debug app.js

--debug and--DEBUG-BRK parameters. These two parameters will only start debugger listening mode, will not enter the command line debug mode, and the former will run all the code, usually can be used for event debugging, the latter will be interrupted when entering, convenient to start debugging from the beginning.

Detailed debug instructions Enter Help or reference official documents in debug mode, https://nodejs.org/dist/latest-v7.x/docs/api/debugger.html

After trying, the efficiency is moving, the operation is a bit cumbersome, the command line interface to do debugging is always a bit unfriendly

B.node-inspector

Node-inspector allows us to debug breakpoints inside the Devtools.

The node. JS Native Debugger module uses V8-debug protcol, and Devtools uses chromedebugging protcol. So Node-inspector played a role in translating and conveying.

Installing Node-inspector

Npminstall-g Node-inspector

Run Node-inspector The default debug port is 5858, listening on 8080 ports,

node-inspector&

You can also modify the port

Node-inspector--web-port 8088--debug-port 5859

The--web-port here is the Debug page address port of Chrome Devtools,--debug-port the built-in debug port for NodeJS boot

We can open the page in http://localhost:8080/debug?port=5858, debug the program opened with the--debug (-BRK) parameter

C, node's latest mode method

V8inspector integration allows DevTools to directly connect to node. js Debugger for debugging.

The new version of Chrome and the new version of node. JS support can communicate directly with each other through a new debug protocol, eliminating the need for node-inspector.
Nodejs 6.3+

Chrome 55+

Run node--inspect app.js

Open the output address in chrome, such as

Chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html? Experiments=true&v8onl

y=true&ws=127.0.0.1:9229/9c1562a2-7f51-4605-a77d-4217cbfb4965

Open a new tab or window and go to the page for debugging, such as

http://localhost:8999

Because this built-in debug is strong enough, Node-inspector is not updated.

However, I was not satisfied, and then found the Vscode

D, Vscode

In the Vscode can be very convenient similar to the IDE to debug node, see figure

Open project into debug, direct run, request interface or page

Enter the breakpoint, the Debug interface is as follows

You can also attach process and add configuration

How, is not very like Visual Studio, full of intimacy ~

IDE Debugging

This eclipse and webstorm can be easily done.

But I prefer the lightweight editor.

Reference links

http://www.barretlee.com/blog/2015/10/07/debug-nodejs-in-command-line/

Code debugging and performance tuning for Nodejs

http://i5ting.github.io/node-debug-tutorial/
Node-debug Tutorial

Https://www.npmjs.com/package/node-inspector
Node-inspector

Https://www.madcoder.cn/node-debug-inspect.html
Latest node. js Debug Dafa-v8_inspector

Https://nodejs.org/dist/latest-v7.x/docs/api/debugger.html
Nodejs Official documents

Nodejs Debugging methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.