I would like to know what "module wrapper function" means and what it does to my code.
(function (exports, require, module, __filename, __dirname) {
});
I would like to know what "module wrapper function" means and what it does to my code.
(function (exports, require, module, __filename, __dirname) {
});
Share
Improve this question
edited Apr 16, 2022 at 12:14
Finn Smith
8832 gold badges8 silver badges22 bronze badges
asked Jul 15, 2018 at 15:50
yasiruyasiru
1551 gold badge1 silver badge11 bronze badges
2
|
3 Answers
Reset to default 14Original 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
anduncaughtException
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
- It emits events such as
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 accesssomething
and it will betrue
(without having to export it). - Read more at the Node
global
documentation.
- For example, if you do
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];
});
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.
If you write some code, e.g.:
const fs = require("fs");
module.exports = fs.readFileSync(__dirname + "test.txt");
process.exit();
Then you work with a lot of global variables that are not defined through the js spec, they are only available in node. To make them available to the code, the code you executed gets enclosed in the function you quoted which allows you to access them. They could also have written some C++ code to solve that but that would have been far more complicated.
process
andglobal
? The wrapper in the node.js doc, does not include those. The other arguments are module-specific variables so this is how node.js makes them uniquely defined for each module by inserting your module code inside this function before the code is processed and then it calls your code via this function and passes it these module-specific arguments. – jfriend00 Commented Jul 15, 2018 at 16:11