Hacker News new | ask | show | jobs
by tgasson 5128 days ago
Every module will have a `module` variable available to it.

    `module` defaults to {
        id: ...
        exports: {}
        parent:...
        filename:...
        loaded:...
        exited:...
        children:...
        paths:...
    }
Whatever module.exports is at the end of the module is passed to the require statement.

Every file essentially has two lines at the top.

    this = module.exports;
    var exports = module.exports;
The first helps lazy developers by allowing exporting without explicitly saying so. Because of this, the following alone is a valid module that exports {foo:'bar'}

    foo = 'bar'; // can also be written as this.foo = 'bar';
And the second matches the CommonJS standard. So the following CommonJS module will work:

    exports.foo = 'bar';
If you assign module.exports at any time after using `this` or `exports`, module.exports will point to the new object, and anything on the original module.exports object wont be passed to the `require` call.