Every time you require a file in node, these are the 5 things that take place:
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 it’s 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.
Note: I learned this thanks to Sameer Buna’s “You don’t know node” talk from ForwardJS San Francisco 2017.