eolas/zk/Module_wrapping_at_runtime.md

24 lines
622 B
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
tags:
- node-js
---
## The Module Wrapper Function
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**
Note that one of these parameters is the
[module object](Modules.md#structure-of-a-module).
2022-04-23 13:26:53 +01: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.