|
@ -27,26 +27,33 @@ The contents of `circle.js`: |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
The module `circle.js` has exported the functions `area()` and |
|
|
The module `circle.js` has exported the functions `area()` and |
|
|
`circumference()`. To export an object, add to the special `exports` |
|
|
`circumference()`. To add functions and objects to the root of your module, |
|
|
object. |
|
|
you can add them to the special `exports` object. |
|
|
|
|
|
|
|
|
Note that `exports` is a reference to `module.exports` making it suitable |
|
|
Variables local to the module will be private, as though the module was wrapped |
|
|
for augmentation only. If you are exporting a single item such as a |
|
|
in a function. In this example the variable `PI` is private to `circle.js`. |
|
|
constructor you will want to use `module.exports` directly instead. |
|
|
|
|
|
|
|
|
|
|
|
function MyConstructor (opts) { |
|
|
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 |
|
|
} |
|
|
instead of building it one property at a time, assign it to `module.exports` |
|
|
|
|
|
instead of `exports`. |
|
|
|
|
|
|
|
|
|
|
|
Below, `bar.js` makes use of the `square` module, which exports a constructor: |
|
|
|
|
|
|
|
|
// BROKEN: Does not modify exports |
|
|
var square = require('./square.js'); |
|
|
exports = MyConstructor; |
|
|
var mySquare = square(2); |
|
|
|
|
|
console.log('The area of my square is ' + mySquare.area()); |
|
|
|
|
|
|
|
|
// exports the constructor properly |
|
|
The `square` module is defined in `square.js`: |
|
|
module.exports = MyConstructor; |
|
|
|
|
|
|
|
|
|
|
|
Variables |
|
|
// assigning to exports will not modify module, must use module.exports |
|
|
local to the module will be private. In this example the variable `PI` is |
|
|
module.exports = function(width) { |
|
|
private to `circle.js`. |
|
|
return { |
|
|
|
|
|
area: function() { |
|
|
|
|
|
return width * width; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
The module system is implemented in the `require("module")` module. |
|
|
The module system is implemented in the `require("module")` module. |
|
|
|
|
|
|
|
@ -232,18 +239,21 @@ would resolve to different files. |
|
|
* {Object} |
|
|
* {Object} |
|
|
|
|
|
|
|
|
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. For convenience, `module.exports` is |
|
|
`module.exports` is accessible via the `exports` module-global. |
|
|
also accessible via the `exports` module-global. `module` isn't actually |
|
|
`module` isn't actually a global but rather local to each module. |
|
|
a global but rather local to each module. |
|
|
|
|
|
|
|
|
### module.exports |
|
|
### module.exports |
|
|
|
|
|
|
|
|
* {Object} |
|
|
* {Object} |
|
|
|
|
|
|
|
|
The `module.exports` object is created by the Module system. Sometimes this is not |
|
|
The `module.exports` object is created by the Module system. Sometimes this is not |
|
|
acceptable, many want their module to be an instance of some class. To do this |
|
|
acceptable; many want their module to be an instance of some class. To do this |
|
|
assign the desired export object to `module.exports`. For example suppose we |
|
|
assign the desired export object to `module.exports`. Note that assigning the |
|
|
were making a module called `a.js` |
|
|
desired object to `exports` will simply rebind the local `exports` variable, |
|
|
|
|
|
which is probably not what you want to do. |
|
|
|
|
|
|
|
|
|
|
|
For example suppose we were making a module called `a.js` |
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter; |
|
|
var EventEmitter = require('events').EventEmitter; |
|
|
|
|
|
|
|
@ -277,6 +287,28 @@ y.js: |
|
|
var x = require('./x'); |
|
|
var x = require('./x'); |
|
|
console.log(x.a); |
|
|
console.log(x.a); |
|
|
|
|
|
|
|
|
|
|
|
#### exports alias |
|
|
|
|
|
|
|
|
|
|
|
The `exports` variable that is available within a module starts as a reference |
|
|
|
|
|
to `module.exports`. As with any variable, if you assign a new value to it, it |
|
|
|
|
|
is no longer bound to the previous value. |
|
|
|
|
|
|
|
|
|
|
|
To illustrate the behaviour, imagine this hypothetical implementation of |
|
|
|
|
|
`require()`: |
|
|
|
|
|
|
|
|
|
|
|
function require(...) { |
|
|
|
|
|
// ... |
|
|
|
|
|
function (module, exports) { |
|
|
|
|
|
// Your module code here |
|
|
|
|
|
exports = some_func; // re-assigns exports, exports is no longer |
|
|
|
|
|
// a shortcut, and nothing is exported. |
|
|
|
|
|
module.exports = some_func; // makes your module export 0 |
|
|
|
|
|
} (module, module.exports); |
|
|
|
|
|
return module; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
As a guideline, if the relationship between `exports` and `module.exports` |
|
|
|
|
|
seems like magic to you, ignore `exports` and only use `module.exports`. |
|
|
|
|
|
|
|
|
### module.require(id) |
|
|
### module.require(id) |
|
|
|
|
|
|
|
|