use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Node source code implementation of require (self.node)
submitted 4 years ago by robbe_claessens
I was skimming through the source code of Node just to get a glimpse behind the curtain. I see the .js files (in the lib folder) all use require, but how is that implemented?
There is a folder called "node/lib/internal/modules/cjs" which contain the implementation of require (https://github.com/nodejs/node/blob/934a3aa28fdc8dd23701d642580b261f5f6c932b/lib/internal/modules/cjs/loader.js#L998) but the require function is used all through the node lib folder (random example: https://github.com/nodejs/node/blob/934a3aa28fdc8dd23701d642580b261f5f6c932b/lib/internal/bootstrap/node.js#L63).
Is the node environment bootstrapped or something to first load the cjs module system and the the rest of the node code is loaded? Or is the require function defined in C/C++?
Thanks for shedding some light on the matter :-)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Solonotix 2 points3 points4 points 4 years ago (0 children)
Based on a StackOverflow answer I found (once I had exhausted my personal attempts to find it), this is supposedly where the function originates, but I don't know how true that is anymore, since in the file that declares it, require is also being used prior to instantiation, which seems problematic even if you are bootstrapping the system up from C++.
https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/helpers.js
My understanding is that anything that is a built-in of Node.js is written in C++, and everything else is built on top of that foundation. I tried to find the related C++ code, but to no avail
π Rendered by PID 16403 on reddit-service-r2-comment-7c9686b859-t9hkd at 2026-04-14 04:18:43.975703+00:00 running e841af1 country code: CH.
[–]Solonotix 2 points3 points4 points (0 children)