Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

The `exports` and `module.exports` globals aren't as interchangeable as documented #5228

Closed
rmg opened this Issue Apr 6, 2013 · 6 comments

Comments

Projects
None yet
4 participants
Member

rmg commented Apr 6, 2013

According to the docs, exports is a global-like alias for module.exports, but in practice they are not interchangeable. It doesn't take much thinking to see why that is the case (exports no longer references module.exports if you re-assign it).

Examples:

pluseone.js

exports = function(a) { return a+1 }

plustwo.js

module.exports = function(a) { return a+2 }

plusthree.js

exports.plusthree = function(a) { return a+3 }

plusfour.js

module.exports.plusfour = function(a) { return a+4 }

REPL (v0.10.3)

> one = require('./plusone.js');
{}
> two = require('./plustwo.js');
[Function]
> three = require('./plusthree.js');
{ plusthree: [Function] }
> four = require("./plusfour.js");
{ plusfour: [Function] }

I think either the docs need touching up, or there needs to be some hoop jumping added to handle the exports = Constructor shorthand. Maybe exports should be removed from the docs entirely to discourage use?

what's basically happening is:

var module = {
  exports: {}
}

;(function myFile(module, exports) {
  exports = 'something'
})(module, module.exports)

module.exports === {}

so simply setting exports inside myFile doesn't actually do anything due to function scoping. you need to edit either exports or module's attributes for any changes to take effect.

sure, docs on this would be helpful, but nothing has to change. http://nodejs.org/api/modules.html#modules_module_exports doesn't really explain this.

Member

rmg commented Apr 6, 2013

@jonathanong that's a better way of explaining what I meant, thanks.

I found this because it bit me in the butt earlier today when I refactored a module to export a single constructor and everything blew up. It was relatively easy to find the root of the problem once I realized the docs were.. incomplete.

It's the requirement that exports be modified instead of replaced while module.exports does not have that restriction that should be mentioned in the docs. If I come up with a better way to say that in the docs, I'll submit a pull request for it.

andrewrk commented Apr 7, 2013

The docs are probably the easiest thing to get an accepted pull request for - go for it!

Member

rmg commented Apr 8, 2013

I've got a branch for it. Submitting a pull request for stable shortly..

Member

rmg commented Jun 16, 2013

My commits went in (93391ae and 1deeab2), so this can probably be closed. Not sure if closing is best done by submitters or bug wranglers. I'll close in a couple days if I see no responses.

I noticed a couple other weirdnesses in the docs, but a new issue will be created for them when I submit a pull request.

Thanks for following up, if you know your issue has been fixed or no longer valid, feel free to close -- but just make sure to include your reasons for closing, the perfect example is the fact that you included the commits your fixes landed in.

@tjfontaine tjfontaine closed this Jun 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment