🕵️‍♀️Node.js module wrapper function🕵️‍♀️

🕵️‍♀️Node.js module wrapper function🕵️‍♀️

Know how Node.js executes the code under the hood.

Hi Guys👋,

Today we are going to see one of the module of node.js which is module Wrapper

Hey Chhakuli, What is this module wrapper?🤷‍♀️

Node.js module wrapper function?🙄

NodeJS does not run our code directly, it wraps the entire code inside a function before execution. This function is termed a Module Wrapper Function.

How actually it looks:🧠

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

The variables and functions in the node.js are private to the module that has it and not visible to the outside.

The variables and functions are made private in node.js using the node.js module wrapper function.

By doing this, node.js achieves a few things:

  • It keeps top-level variables (defined with var, let and const)scoped to the module rather than the global object.
  • It helps to provide some global -looking variables that are actually specific to the module, such as
    • The modules and exports object that the implementor can use to export values from the module.
    • The convenience variables __filename and __dirname, containing the module's absolute filename and directory path.

So now you know 😄the variables and functions we define in a module are scoped to that module they are private and not visible from the outside.

Deep Dive🤫

If you run a file that has only one line of code in the Node.js environment.


It would return a reference error saying arguments is not defined. But that is not the reality🤯. If you actually try to run it, it would give you some output that would look like the one shown in the image below👇.


In Node.js every file has a wraps every file executes with a function and thus console.log(arguments) prints the 5 arguments that the node passes to that wrapping function.

5 arguments in detail

  • Here, as we see the wrapper function has 5 arguments namely,
  1. export
  2. module
  3. require
  4. __filename
  5. __dirname

let's see these parameters in detail.

  • ☝️exports: A reference to the module that is shorter to type
  • ✌️require: Used to import modules.
  • 👌module: A reference to the current module.
  • 🖖__dirname: The directory name of the current module.This is the same as the path.dirname() of the __filename.


  • 🖐️__filename: The file name of the current module. This is the current module file absolute path with symlinks resolved.




Node.js is an object-oriented open-source platform. The code is executed in such a way that the first code is wrapped inside a function called Node.js module wrapper. These functions that are created during the execution of the code have various parameters.

Did you find this article valuable?

Support Chhakuli Zingare by becoming a sponsor. Any amount is appreciated!