2022-04-23 13:26:53 +01:00
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- node-js
|
|
|
|
---
|
|
|
|
|
2024-10-18 20:00:02 +01:00
|
|
|
# Module wrapping at runtime in NodeJS
|
|
|
|
|
2022-04-23 13:26:53 +01:00
|
|
|
## The Module Wrapper Function
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
When Node runs each of our module files are wrapped within an
|
|
|
|
immediately-invoked function expression that has the following parameters:
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
```js
|
2022-04-23 13:26:53 +01:00
|
|
|
(function (exports, require, module, __filename, __dirname))
|
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
```
|
2022-04-23 13:26:53 +01:00
|
|
|
|
|
|
|
This is called the **module wrapper function**
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Note that one of these parameters is the
|
2024-06-16 18:30:03 +01:00
|
|
|
[module object](Modules_in_NodeJS.md#structure-of-a-module).
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Within any module we can access these parameters: you can think of them as
|
|
|
|
metadata about the module itself. `__filename` and `__dirname` are particularly
|
|
|
|
useful when writing to files and modifying directories.
|