-1

Using RequireJS with CommonJS modules, what happens when I do this:

define(function(require, exports, module) {
    //Put traditional CommonJS module content here

    var simpleCommonJSModule = require('simple-commonjs-module');   

    module.exports = new String('foo');

   return {
        //return empty object along with using module.exports
   }
});

if I return something, I assume the module.exports will be ignored? Or is it the other way around?

1 Answer 1

1

Yes, if you return something module.exports will ignored.

Here's a snippet from the original documentation.

define(function(require, exports, module) {
       var a = require('a'),
           b = require('b');

       //Return the module value
       return function () {};
    } 
);

If you want to use the exports CJS style here's you do it

define(function(require, exports, module) {
   exports.foo = function () {
       return a.bar();
   };
});
1
  • yeah so I guess is this is how AMD/RequireJS shims CommonJS modules, simply by wrapping them with the define function and then returning a value after the exports statements Commented Jun 23, 2015 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.