Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does mean by node module wrapper function in node

I would like to know what "module wrapper function" means and what it does to my code.

(function (exports, require, module, __filename, __dirname) {  
       
    
 });
like image 673
yasiru Avatar asked Jul 15 '18 15:07

yasiru


2 Answers

Original Answer

According to the Node.js documentation,

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

(function(exports, require, module, __filename, __dirname) { 
    // Module code actually lives in here 
}); 

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

  • It keeps top-level variables (defined with var, const or let) 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 module and exports objects 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.

Essentially, this wrapper is used to configure your module, and it enables the use of the variables exports, require, module, __filename, and __dirname.

Edit

OP also mentions the process and global variables.

  • The process object gives information about, and control over, the current Node.js process.
    • It emits events such as exit and uncaughtException to manage the Node process.
    • It also includes functions such as process.abort() to end the current process.
    • To see all of the information, see the Node documentation on process
  • global provides a system for accessing and setting global variables.
    • For example, if you do global.something = true in one module, in another module you can access something and it will be true (without having to export it).
    • Read more at the Node global documentation.

Edit 2

You can edit the wrapper, too:

let Module = require('module');

Module.wrap = (function (exports, require, module, __filename, __dirname) {
    // What you want the new wrapper to be.
    return Module.wrapper[0] + exports + 'console.log("This is the wrapper.");' + Module.wrapper[1];
});
like image 196
Marco Avatar answered Nov 04 '22 13:11

Marco


I think, I'm little late on this post but I'd like to share my 2 cents here.

So the expression you have written is IIFE (Immediately Invoked Function Expression).

Basically, your code in a (Node)file is wrapped inside this particular function. When someone requires this file, IIFE runs automatically and provides you objects such as module.exports, exports, __dirname, __filename.

These objects are not global but local to your module (file). And these are made available by this IIFE function. Using this object can export your module.

Link to the documentation is already provided in above answer, that should help.

like image 26
Sushil Kadu Avatar answered Nov 04 '22 12:11

Sushil Kadu