Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: mention existence/purpose of module wrapper #6433

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
doc: mention existence/purpose of module wrapper
Included a block in the modules.md file to explain the existence and
purpose of the module wrapper.
mtharrison committed May 3, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 696fc7178b7edfd3906c4bed4a25981ca37600bc
29 changes: 27 additions & 2 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
@@ -30,8 +30,9 @@ The module `circle.js` has exported the functions `area()` and
`circumference()`. To add functions and objects to the root of your module,
you can add them to the special `exports` object.

Variables local to the module will be private, as though the module was wrapped
in a function. In this example the variable `PI` is private to `circle.js`.
Variables local to the module will be private, because the module is wrapped
in a function by Node.js (see [module wrapper](#modules_the_module_wrapper)).
In this example, the variable `PI` is private to `circle.js`.

If you want the root of your module's export to be a function (such as a
constructor) or if you want to export a complete object in one assignment
@@ -425,6 +426,30 @@ These are mostly for historic reasons. **You are highly encouraged
to place your dependencies locally in `node_modules` folders.** They
will be loaded faster, and more reliably.

## The module wrapper

<!-- type=misc -->

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

```js
(function (exports, require, module, __filename, __dirname) {
// Your 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you end these list items with periods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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.

## The `module` Object

<!-- type=var -->