Table of contents
1.
Introduction
2.
What is the node.js module wrapper function?
3.
Arguments
4.
Modifying node.js module wrapper function
5.
Frequently asked questions
6.
Key takeaways
Last Updated: Mar 27, 2024

Node.js module wrapper function

Author Ranjul Arumadi
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 
});
You can also try this code with Online Javascript Compiler
Run 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:-

Arguments Description
exports It refers to module.exports which are shorter to types.
requires(id) The parameter is a string that refers to the module name or path.
module It implies the current module that is executed.
__filename It implies the file name of the current module.
__dirname It 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);
You can also try this code with Online Javascript Compiler
Run Code

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");
You can also try this code with Online Javascript Compiler
Run Code

 

2. module.js

console.log("Hello Ninja!");
You can also try this code with Online Javascript Compiler
Run Code

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!

 

Also see - Difference between argument and parameter and Difference Between Controller and Restcontroller

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?.

Live masterclass