top of page
Search
  • Writer's pictureYun'Harla

Debug / Edit Node.JS Internals


NodeWeekly.com linked to an interesting article: How Does Node Load Its Built-In/Native Modules? by Joyee Cheung - discussing briefly debugging and editing Node.JS' internal JS codes.


It's a great read, so check it out. And here's my 2 cents worth...




Debugging Node.JS Internals

Without having to rebuild your own copy of node.exe, simply debug using Chrome Debugger (i.e. chrome://inspect) aka DevTools for Node, or VS Code.


You can (1) browse Node.JS Internals from the JS files tree in the left panel, or (2) use CTRL-P to find a quick specific file. Then, simply set your breakpoints and have fun stepping through Node.JS Internals.

  • Of course, it helps if you have Node.JS source codes and look up the file of interest in the /libs folder beforehand. I.e. be prepared.

    • If not, you can also step in from your codes, deep into the internals.


Editing Node.JS Internals

2 feasible ways here.


Live Edit - Code Replacements

If you're just looking to make minor code tweaks to see how things work out, just try to override in your codes, or live in a debugger.


Essentially, anything not 'const' can be overwritten. Functions are great candidates to be replaced.


E.g.: replacing console.log to dump stack trace when string to be printed starts with a keyword, say 'Warning'. This code swap can be in your codes, so an offline edit.

console._log = console.log; //save the original fn for use console.log = function(s) { //override with your mod if ((typeof s === 'string') && (s.indexOf('Warning') === 0)) { console.trace(); //print stack trace } console._log.apply(this, arguments); };

PS: above is useful to figuring out where weird/unexpected printouts come from.


Use a live edit in debugger to make changes multiple times for live tweaks. This should work for internal file variables not otherwise easily replaceable - when you pause in a breakpoint in that file.


Once you've figured out your mod/patch, formalize it via source edit.


Source Edit - Hitchhiking Codes

Edits of existing Node.JS internals is easy. It's just the build step of node.exe that takes a long time (for a fresh build).


Joyee's article mentions briefly how Node.JS has a python script to read the tons of built-ins. Wish there's more detailed info on how to add new modules and files.


Fear not! A simpler way I've found is simply to hitchhike your way in. Refer to my Node.JS+ mod for examples.


In short:

  • Add your codes to an existing Node.JS internals JS file, so that they get built into node.exe easily, without having to mess with the build system.

    • E.g. /lib/internal/bootstrap/pre_execution.js is a good 'early' place to inject your codes.

    • Just be aware that some core modules we take for granted may not yet be available so early, so look in the original file for how those modules are require'd. And adjust accordingly.

  • For modules (say dependencies you need to use), do this extra preparation step: pack and minify (e.g.: using browserify + minifyify) each module into way fewer lines of minified codes.


Have fun modding Node.JS : )

Recent Posts

See All
Post: Blog2_Post
bottom of page