mirror of https://github.com/nodejs/node.git
doc: clarify exports and module.exports
When exporting a single function you must use `module.exports` instead of the `exports` convenience reference.archived-io.js-v0.10
parent
6bcf51e030
commit
93391ae9cb
|
@ -133,9 +133,10 @@ See the [module system documentation][] for more information.
|
||||||
|
|
||||||
<!-- type=var -->
|
<!-- type=var -->
|
||||||
|
|
||||||
An object which is shared between all instances of the current module and
|
A reference to the `module.exports` object which is shared between all
|
||||||
made accessible through `require()`.
|
instances of the current module and made accessible through `require()`.
|
||||||
`exports` is the same as the `module.exports` object.
|
See [module system documentation][] for details on when to use `exports` and
|
||||||
|
when to use `module.exports`.
|
||||||
`exports` isn't actually a global but rather local to each module.
|
`exports` isn't actually a global but rather local to each module.
|
||||||
|
|
||||||
See the [module system documentation][] for more information.
|
See the [module system documentation][] for more information.
|
||||||
|
|
|
@ -30,6 +30,20 @@ The module `circle.js` has exported the functions `area()` and
|
||||||
`circumference()`. To export an object, add to the special `exports`
|
`circumference()`. To export an object, add to the special `exports`
|
||||||
object.
|
object.
|
||||||
|
|
||||||
|
Note that `exports` is a reference to `module.exports` making it suitable
|
||||||
|
for augmentation only. If you are exporting a single item such as a
|
||||||
|
constructor you will want to use `module.exports` directly instead.
|
||||||
|
|
||||||
|
function MyConstructor (opts) {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
|
||||||
|
// BROKEN: Does not modify exports
|
||||||
|
exports = MyConstructor;
|
||||||
|
|
||||||
|
// exports the constructor properly
|
||||||
|
module.exports = MyConstructor;
|
||||||
|
|
||||||
Variables
|
Variables
|
||||||
local to the module will be private. In this example the variable `PI` is
|
local to the module will be private. In this example the variable `PI` is
|
||||||
private to `circle.js`.
|
private to `circle.js`.
|
||||||
|
@ -219,7 +233,7 @@ would resolve to different files.
|
||||||
|
|
||||||
In each module, the `module` free variable is a reference to the object
|
In each module, the `module` free variable is a reference to the object
|
||||||
representing the current module. In particular
|
representing the current module. In particular
|
||||||
`module.exports` is the same as the `exports` object.
|
`module.exports` is accessible via the `exports` module-global.
|
||||||
`module` isn't actually a global but rather local to each module.
|
`module` isn't actually a global but rather local to each module.
|
||||||
|
|
||||||
### module.exports
|
### module.exports
|
||||||
|
|
Loading…
Reference in New Issue