DEV Community

loading...

How Requiring Modules Really Works In Nodejs

cpuram1 profile image CALVIN JOB PURAM Updated on ・3 min read

In this article, I will demystify how nodejs module 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 systems 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 a popular thing to use. Now you might be wondering why every file in nodejs that we create get access to the require() function and where does it come from and how exactly does it work 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 required 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 required 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

Wrapping

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...
})

Enter fullscreen mode Exit fullscreen mode

So this is where the magic happens here nodejs wrapped all the codes in a file and that is why we have access to (require, exports, module, __filename __dirname) in every file. This implementation makes it possible for all files 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
Enter fullscreen mode Exit fullscreen mode

Execution

Next, the code in the wrapped function gets executed by nodejs runtime

Returning Exports

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

Caching

In the last step, the modules are 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 subsequent calls, the code is simply retrieved from cached. This is important for you to know how it works in other to become a great developer and above all a more independent nodejs developer.

Discussion (3)

pic
Editor guide
Collapse
tastaslim profile image
Taslim Arif

It was a gold buddy. Keep it up

Collapse
tastaslim profile image
Taslim Arif

Few spelling and sentence mistakes are there, I don't complain, it just if you improve them this gold article will shine more. BTW, fantastic job.

Collapse
cpuram1 profile image
CALVIN JOB PURAM Author

Thanks for your feedback I will look into that. This means a lot to me cheers