Node.js module wrapper function

Ranjul Arumadi
Last Updated: May 13, 2022

Introduction

Node.js is an open-source platform for operating both backend and frontendNode.js does not run the code directly but first wraps it inside a function. This wrapping is done in the form of a function, and this function is called the node.js module wrapper function. This blog gives an overview of the node.js module wrapper function.

What is the node.js module wrapper function?

Node.js will wrap a module's code with a wrapper function before it is executed. The wrapper function looks like the following:

Code:

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

The variables and functions in the node.js are private to the module that has it. The variables and functions inside the module are private and not visible to the outside. The variables and functions are made private in node.js using the node.js module wrapper function.

Arguments

The arguments in the wrapper function are as follows:-

ArgumentsDescription
exportsIt refers to module.exports which are shorter to types.
requires(id)The parameter is a string that refers to the module name or path.
moduleIt implies the current module that is executed.
__filenameIt implies the file name of the current module.
__dirnameIt refers to the directory name of the current module.

Note that every file in Node.js will wrap every file that it executes. To make it clear try running the following code in a Node.js environment and observe the output.

Code:

/*
  function(export, module, _filename, _dirname) {
      console.log(arguments);
  }
*/

console.log(arguments);

Node.js wraps the code inside the node.js module wrapper function. Observe the commented code to understand how it works.

Output:

$node main.js
{ '0': {},
 '1': 
 { [Function: require]
   resolve: [Function: resolve],
   main: 
     Module {
       id: '.',
       exports: {},
       parent: null,
       filename: '/home/cg/root/5394193/main.js',
       loaded: false,
       children: [],
       paths: [Object] },
   extensions: { '.js': [Function], '.json': [Function], '.node': [Function] },
   cache: { '/home/cg/root/5394193/main.js': [Object] } },
 '2': 
 Module {
   id: '.',
   exports: {},
   parent: null,
   filename: '/home/cg/root/5394193/main.js',
   loaded: false,
   children: [],
   paths: 
     [ '/home/cg/root/5394193/node_modules',
       '/home/cg/root/node_modules',
       '/home/cg/node_modules',
       '/home/node_modules',
       '/node_modules' ] },
 '3': '/home/cg/root/5394193/main.js',
 '4': '/home/cg/root/5394193' }

Modifying node.js module wrapper function

Consider two files

1. main.js

var Module = require("module");
 
(function (moduleWrapCopy) {
 Module.wrap = function (script) {
  script = "console.log('modified the module wrapper function');" + script;
 
   return moduleWrapCopy(script);
 };
})(Module.wrap);
 
require("./module.js");

 

2. module.js

console.log("Hello Ninja!");

Every time a module is required in the main.js file, we overwrite the module.wrap function to console.log(‘modified the module wrapper function’). It gives a message if the modification is successful or not.

The output will be:

$ node main.js
modified the module wrapper function
Hello Ninja!

Frequently asked questions

1. What are the advantages of the node.js module wrapper function?

The advantages of the node.js module wrapper function are-

  • It keeps top-level variables that are defined with var, const, or let are scoped to the module rather than the global object.
  • It helps to provide some global-looking variables that are actually specific to the module like module, exports, __filename, and __dirname.

 

2. What are the arguments present in the node.js module wrapper function?

The arguments present in the node.js module wrapper functions are exports, require, module, __filename, and __dirname.

Key takeaways

Node.js is a multithreaded, 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.

 

If you loved reading this article about the Node.js module wrapper function, check out What is Node.js? Where, When & How To Use It? and Why every web developer should learn Node.js?.

Was this article helpful ?
2 upvotes