Member-only story

Nodejs: What is the Default wrapper function ??

Sunil Kumar
2 min readMar 11, 2023

--

In Node.js, the CommonJS module system automatically wraps every module in a function. This default wrapper function encapsulates the module’s code and provides a private scope for its variables and functions.

Here’s an example of what the default wrapper function looks like:

(function (exports, require, module, __filename, __dirname) {
// Your module code actually goes here
});

When you define a module in Node.js, the module’s code is executed within this wrapper function, with the following parameters:

  • exports: An object that is used to define what should be exported from the module.
  • require: A function that is used to import other modules into the current module.
  • module: An object that represents the current module and its properties, such as its filename, exports, and so on.
  • __filename: The absolute path of the current module file.
  • __dirname: The absolute path of the directory that contains the current module file.

This default wrapper function is what makes it possible to define private variables and functions within a module that are not accessible from other modules, unless explicitly exported.

--

--

Sunil Kumar
Sunil Kumar

Written by Sunil Kumar

With an extensive professional experience spanning over 16 years in the IT industry, I am a seasoned expert in AWS Cloud, DevOps, FastAPI, and Python.

No responses yet