Module.exports cannot be specified as an argument

I am building my package using Browserify .

I have the following service.js:

(function (exports, require) {

     // ...
     var Service = function (name, region) {
         this.name = name;
         this.region = region;
         // ...
     }

     exports = Service;

})(module.exports, require);

Whenever I try require('./service')on another module, I get an empty object, as if the object exportshad never been set.

If I use module.exportswithout encapsulating the arguments, everything works fine:

(function (require) {

   // ...
     var Service = function (name, region) {
         this.name = name;
         this.region = region;
         // ...
     }

     module.exports = Service;

})(require);

Why is this happening and why is it necessary?

+4
source share
1 answer

example - , , module.exports. exports = Service, , exports, , module.exports.

module.exports = Service, module, .

:

(function (m, require) {

    // ...
    var Service = function (name, region) {
        this.name = name;
        this.region = region;
        // ...
    }

    m.exports = Service;

})(module, require);

m module, m.exports, module.exports, m module .

+2

All Articles