Skip to content

Instantly share code, notes, and snippets.

@starandtina
Forked from nitin42/require.js
Created October 13, 2017 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starandtina/a097b47c3457e7985dcf496971798734 to your computer and use it in GitHub Desktop.
Save starandtina/a097b47c3457e7985dcf496971798734 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