Use Eclipse to debug Node. js Code

Source: Internet
Author: User

Node. js is based on Google V8, and Google V8 has an Eclipse debugging plug-in that also supports node. js.

1. Install Eclipse debugger for V8

This should be known to anyone who has used Eclipse, but let's simply put it:

Select "Install New Software"

Click "Add"

Location: http://chromedevtools.googlecode.com/svn/update/dev/

Name: You like it. You can remember it easily.

Click "OK" and select the site you just added in "Work:

Select "Chromium JavaScript Remote Debugger" and continue to the next step. After the installation is complete, the system prompts you to restart Eclipse. After restarting Eclipse, you can start debugging.

2. How to debug Nodejs?

From the plug-in name "Remote", we can expect that this is a Remote debugging tool. The general process is that node opens a tcp debugging port, then the debugging tool uses this tcp port to communicate with node for debugging.

To enable the debug port of node, you must add the -- debug parameter when executing the node script:

 
 
  1. node --debug[=port] NodeApp.js  
  2.  
  3.  
  4. or  
  5.  
  6. node --debug-brk[=port] NodeApp.js 

Port is the enabled debug port. The default value is 5858.

The difference between -- debug and -- debug-brk is that -- debug runs the script directly when executing the script, but -- debug-brk does not execute the script immediately when executing the script, wait for the connection of the debugger and execute it according to the command of the debugger.

Let's write a simple code for debugging:

Then use node-debug hello_world.js to run:

The output line is debugger listening on port 5858.

5858 is the default port. You can also use -- debug = 5859 to specify different ports.

Now that the node debugging mode is running, configure Eclipse to connect to the node and debug it.

First, we need to add a new V8 VM debugging configuration in debug config:

Right-click and select "new" to create a new configuration item:

Because the debugging port we just set for node is the default 5858 port, here our port is 5858, and our name is "Node-5858 ".

Click debug to start debugging, or use the following method to start debugging:

The debugging mode is probably like this:

(Note: This is the debugging view. Click the bug button in the upper right corner. If you do not have this button in Eclipse, click the Add button on the left of the bug in the upper right corner, find debug, and add it)

After the connection in debug mode is successful, you will see an additional Project named "Node-5858" in your "Project Explorer" (the name set in debug config just now, all the files in the file are debugging files or debugging files require.

Open these files, and you can add a breakpoint to start debugging. For example, if a breakpoint is added to row 8th, the breakpoint is entered every time the browser accesses it.

Other debugging methods are the same as debugging other code using Eclipse.

The debugging shortcut for Eclipse is: F5 is entered in one step, and F6 is skipped in one step.

Finally, let's talk about the debug mode started in debug-brk mode:

We can see that only a line of "debugger listening on port 5858" is output, and "Server running at..." is not output later .....".

This is because the debugging mode started does not execute the code immediately, but waits for the connection from the debugger (it can be understood that a breakpoint is added to the first line of hello_world.js ), we recommend that you practice your own shoes.

Reference: https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger

Original article: http://club.cnodejs.org/topic/4f16442ccae1f4aa27001105

Related Article

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.