Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to debug node internal modules?

For example, I want to add some breakpoints on the internal file _http_server.js,but the file does not really exist on my disk.

What should I do?

like image 347
lx1412 Avatar asked Jun 04 '19 02:06

lx1412


People also ask

Does node have a debugger?

A minimal CLI debugger is available with node inspect myscript.js . Several commercial and open source tools can also connect to the Node.js Inspector.

Can I console log in node modules?

Logging using the console module. The most common way to log in Node. js is by using methods on the console module (such as log() ). It's adequate for basic debugging, and it's already present in the global scope of any Node.

Which node module helps debug node applications?

NODE_DEBUG enables debugging messages using the Node. js util. debuglog (see below), but also consult the documentation of your primary modules and frameworks to discover further options.

How does node work internally?

Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests. Node JS Web Server receives those requests and places them into a Queue. It is known as “Event Queue”. Node JS Web Server internally has a Component, known as “Event Loop”.


1 Answers

Yeah, at compile time the .js files in the source get packed into the node executable, which makes me unsure if/how you can set breakpoints at all on there.

What worked for me:

  1. copy the relevant .js file from a checkout of the node source code to the current directory, e.g., readline.js
  2. change local imports from require('readline') to require('./readline')
  3. use the undocumented node --expose-internals flag to allow the copied .js file to still require('internal/…')
  4. Use console.log statements / debugger on the now-local file to figure out what was happening
like image 52
andrewdotn Avatar answered Oct 05 '22 12:10

andrewdotn