Maximus Maximus - 1 year ago 66
Javascript Question

Why doesn't the following `exports = { z: function() {} };` work


exports = {
z: function() {


require('./a').z(); // error

Why does
return an empty object?

Answer Source

Because in your example you are overriding the exports global, and not updating it. If you replace your code with:

exports.z = function() {

it will work, because you are adding z to the actual exports object.

If you look at the docs, you will see:

A reference to the module.exports that is shorter to type.

So you can imagine this situation:

var module = {
    exports: { /* some export stuff */ }

var exports = module.exports;

Now if you replace exports with exports = something, you aren't actually changing module.exports.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download