Skip to content

Instantly share code, notes, and snippets.

@nitin42
Created October 8, 2017 15:33
Show Gist options
  • Save nitin42/85abed4703739899d4f9f886c5c724aa to your computer and use it in GitHub Desktop.
Save nitin42/85abed4703739899d4f9f886c5c724aa to your computer and use it in GitHub Desktop.
How Node.js's require() works ??
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