Created
October 8, 2017 15:33
-
-
Save nitin42/85abed4703739899d4f9f886c5c724aa to your computer and use it in GitHub Desktop.
How Node.js's require() works ??
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const some_module = require('some_module') | |
/** | |
* require('some_module') calls Module._load | |
* | |
* Module._load then tries to load the module with a filename (also save it to the cache) using module.load(filename) | |
* | |
* module.load(filename), given a filename, passes it to the proper extension handler ('.js', '.json') | |
* | |
* If there were any errors when loading the file, it deletes the file from the cache (delete Module._cache[filename]) and throws an error | |
* | |
* Module._extension then compiles the file by reading the content of the filename (with an extension) | |
* | |
* If there are no errors, it returns module.exports 😄 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment