module
free variable is a reference to the object
representing the current module.
module.exports
is the
same as the exports object.
module
isn't actually a global but rather
local to each module.
Module
system.
Sometimes this is not acceptable,
many want their module to be an instance of some class.
To do this assign the desired export object to module.exports
.
[~/javascript/node.js/creating_modules/module_exports(master)]$ cat a.js var EventEmitter = require('events').EventEmitter; module.exports = new EventEmitter(); // Do some work, and after some time emit // the 'ready' event from the module itself. setTimeout(function() { module.exports.emit('ready'); }, 1000);
$ cat main.js var a = require('./a'); a.on('ready', function() { console.log('module a is ready'); });
$ node main.js module a is ready
module.exports
debe hacerse inmediatamente.
No puede hacerse en un callback.
Casiano Rodriguez León 2015-01-07