How Requiring Modules Really Works In Nodejs

Javascript Apr 05, 2020

In this article, I will talk about how nodejs module actually works behind the scenes. First of all, in nodejs each javascript file is treated as a separate module and nodejs uses the Commonjs Module System (require(), module.exports). There is also the ES Module System used in browsers (import/export). The Commonjs Module System and the ES Module System are two different module system that you will see in different situations. There have been some attempts to bring the ES Module to nodejs (.mjs) but so far it's not really a popular thing to use. Now you might be wondering why in nodejs each and every file that we create get's access to the require() function because the require() function is not a javascript function so where does it come from and how exactly does it works behind the scenes?

What Happens When We Require a Module

In a broad overview whenever we require a module by calling the require() function with the module name as the argument require('test-module'), the following steps are executed behind the scenes

  • The path to the require module is resolved and the file is loaded.
  • The process called wrapping happens.
  • Then the module code is executed.
  • The module exports are returned.
  • The entire module gets cached.

So let's look at each step in detail.

Resolving and Loading

How does nodejs knows where to locate the file when we require it? remember we can load three different types of modules:

  • nodejs core modules require('crypto')
  • developer modules require('./controller/post')
  • third party modules require('express')

So this process is known as resolving the file path and this is how it works
when the require() receives the module name as a parameter, it will

  • First try to load a core module with that name
  • If the path starts with './' or '../' it means that it's a developer module because we indicate the relative path to our file so nodejs will load that file but if there is no file with that name then it will look for a folder instead and load index.js file.
  • Finally if the require module is either a core module or a developer module, nodejs will assume that it is a module from npm and so nodejs will step into node_modules and try to find the module and load it.
  • Lastly, if the file can't be found anywhere, then an error is thrown and execution is done


After the module is loaded, the module code is wrapped into a special function which will give us access to some special objects

(function(exports, require, module, __filename, __dirname){
  //module code...

So this is where the magic happens here nodejs wrapped all the codes in a file and that is why we have access to the require, exports, module, __filename __dirname in every file. This implementation makes it possible for all file to have a private scope.

// require- function to require a module
// module- reference to the current module
// exports- a reference to module.exports, used to exports objects from a module
// __filename- absolute path to the current module file
// __dirname- directory name of the current module


Next, the code in the wrapped function get's executed by nodejs runtime

Returning Exports

The require function returns exports of the require module and these exports are stored in the module.exports objects.


The last step is that modules are actually cached after the first time they are loaded what this means is that if you require the same module multiple times you will always get the same results and the code in the module is only executed in the first call and in subsequent calls, the code is simply retrieved from cached. This is really important for you to know how it really works in other to become a great developer and above all a more independent nodejs developer.

Calvin puram

I thrive to design and develop ideas into a project that makes life easy, solve problems, and implement innovations.