How does require() in node.js work?

Source code is here. exports/require are not keywords, but global variables. Your main script is wrapped before start in a function which has all the globals like require, process etc in its context.

Note that while module.js itself is using require(), that’s a different require function, and it is defined in the file called “node.js”

Side effect of above: it’s perfectly fine to have “return” statement in the middle of your module (not belonging to any function), effectively “commenting out” rest of the code

Leave a Comment