简体   繁体   中英

RequireJS with CommonJS modules

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?

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();
   };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM