5 things that happen every time you require a file in node

Every time you require a file in node, these are the 5 things that take place:

  • Resolve
  • Load
  • Wrap
  • Evaluate
  • Cache


Node will attempt to map the string given to require() into a path on the file system. This path could be local to node, under nodule_modules folder, or node modules under a parent directory or any other path.


Node will then load the contents of this file into memory


Wraps the content of the file with an IIFE (I've discussed this IIFE in another blog post How require and module.exports in node works)


Node will then evaluate the file (for eg: using the V8 engine)


Once its done evaluating the file, it will cache it. The next time you require the same file, these steps will not happen and it will read it from the cache directly.