Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'module' variable for modules #520

Closed
gfwilliams opened this issue Apr 17, 2015 · 4 comments
Closed

Add 'module' variable for modules #520

gfwilliams opened this issue Apr 17, 2015 · 4 comments

Comments

@gfwilliams
Copy link
Member

@gfwilliams gfwilliams commented Apr 17, 2015

http://forum.espruino.com/comments/12223159/

This should work - currently you need to use just exports:

// the module
module.exports = function () {
  console.log('I am the module');
};
// the consumer
var module = require('my-module');
module(); // I am the module
@gfwilliams
Copy link
Member Author

@gfwilliams gfwilliams commented Apr 17, 2015

And also ensure that this is set correctly: http://forum.espruino.com/comments/12223222/

@WebReflection
Copy link

@WebReflection WebReflection commented Apr 17, 2015

please bear in mind that reassigning exports does not actually export a thing in node.js, that is why people use module.exports = ... instead.

exports = function wut() {};

If we require that file in node the exported value would be just a regular empty object.

In order to actually overwrite the exports you need to overwrite the module property.

module.exports = function wut() {};

Inside a module, and only when required, exports is the equivalent of this

However, in the main file, this will be the global object instead.
An old trick to know if a file is loaded as module is indeed:

function log(s) {
  console.log(s);
}
if (this === global) {
  log('main');
} else {
  module.exports = log;
}
@WebReflection
Copy link

@WebReflection WebReflection commented Apr 17, 2015

As summary, this is how in JS you would evaluate a module:

function evalRequire(scriptContent) {
  var exports = {};
  var module = {exports: exports};

  Function(
    'global,exports,module',
    scriptContent
  ).call(
    exports,
    global,
    exports,
    module
  );

  return module.exports;
}

As you can see, replacing the exports variable inside the module won't actually affect the exported value, since that's retrieved through the module and exports is simply a shortcut.

node.js has been like this since version 0.4 if I remember correctly, hope this helps.

@gfwilliams
Copy link
Member Author

@gfwilliams gfwilliams commented Apr 17, 2015

Thanks, yes, it does. I'm actually pretty sure I added the exports= behaviour at someone's request. Looks like I should have double-checked first!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.