Skip to content

Commit 2e22fa0

Browse files
sam-githubaddaleax
authored andcommitted
doc: improve description of module exports
- Do not use word alias, it isn't well defined - Fix return value of require() example, which confusingly was not the exported API as it should have been. - Fix the require() example, which claimed that the module exported `0`, when it exports `some_func`. - Describe best practice in keeping exports and module.exports bound together. - Describe why exports exists - Remove reference to magic, which is also not well defined PR-URL: #9622 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 70633f9 commit 2e22fa0

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

doc/api/modules.md

+31-14
Original file line numberDiff line numberDiff line change
@@ -529,34 +529,51 @@ const x = require('./x');
529529
console.log(x.a);
530530
```
531531

532-
#### exports alias
532+
#### exports shortcut
533533
<!-- YAML
534534
added: v0.1.16
535535
-->
536536

537-
The `exports` variable that is available within a module starts as a reference
538-
to `module.exports`. As with any variable, if you assign a new value to it, it
539-
is no longer bound to the previous value.
537+
The `exports` variable is available within a module's file-level scope, and is
538+
assigned the value of `module.exports` before the module is evaluated.
539+
540+
It allows a shortcut, so that `module.exports.f = ...` can be written more
541+
succinctly as `exports.f = ...`. However, be aware that like any variable, if a
542+
new value is assigned to `exports`, it is no longer bound to `module.exports`:
543+
544+
```js
545+
module.exports.hello = true; // Exported from require of module
546+
exports = { hello: false }; // Not exported, only available in the module
547+
```
548+
549+
When the `module.exports` property is being completely replaced by a new
550+
object, it is common to also reassign `exports`, for example:
551+
552+
```js
553+
module.exports = exports = function Constructor() {
554+
// ... etc.
555+
```
540556
541557
To illustrate the behavior, imagine this hypothetical implementation of
542-
`require()`:
558+
`require()`, which is quite similar to what is actually done by `require()`:
543559
544560
```js
545561
function require(...) {
546-
// ...
562+
var module = { exports: {} };
547563
((module, exports) => {
548-
// Your module code here
549-
exports = some_func; // re-assigns exports, exports is no longer
550-
// a shortcut, and nothing is exported.
551-
module.exports = some_func; // makes your module export 0
564+
// Your module code here. In this example, define a function.
565+
function some_func() {};
566+
exports = some_func;
567+
// At this point, exports is no longer a shortcut to module.exports, and
568+
// this module will still export an empty default object.
569+
module.exports = some_func;
570+
// At this point, the module will now export some_func, instead of the
571+
// default object.
552572
})(module, module.exports);
553-
return module;
573+
return module.exports;
554574
}
555575
```
556576
557-
As a guideline, if the relationship between `exports` and `module.exports`
558-
seems like magic to you, ignore `exports` and only use `module.exports`.
559-
560577
### module.filename
561578
<!-- YAML
562579
added: v0.1.16

0 commit comments

Comments
 (0)