Search code examples
javascriptnode.jsv8embedded-v8

are *.js files in node/lib used during compilation of the node executable?


How are the files in Node.js source code's lib directory used by node? Does the node executable interpret the files in the library before running or are these javascript files some how used during compilation of the node executable?


Solution

  • Those files are what are termed internal Javascript files. They are packaged into the node executable and node.js knows how to get them from the executable when needed during the running of a node.js app. Executable files contain a resource system so that in addition to code, they can also contain other types of resources (text, images, dialogs, etc...).

    When you do require() in a node.js script file, it checks the name you're looking for against a list of known internal script filenames. If it matches, then it fetches the source from its internal location in the executable file, not from a separate file in your local file system. Similarly, if the require() is coming from within one of these internal files, it knows to look for the required file in its internal location too.

    They are run as Javascript at the time of execution. They are not precompiled into something other than Javascript. The main difference is that they are Javascript script resources contained within the node executable, not something loaded from the file system.