score:5

Accepted answer

usually when you get an empty object from a require call, it's because you have circular dependencies. so you require a, which requires b, which requires c, which requires a. in that case c will get an empty object representing a, because a hasn't finished exporting its functions/objects yet and a will only be fully available to c on the next tick, when a has finished exporting.

here's an example:

// a.js
var b = require('./b');
module.exports = {
  dostuff: function () {

  }
}

// b.js
var c = require('./c');

// c.js
var a = require('./a');

// this will fail because a.js hasn't exported dostuff yet, since a required b, which 
// required c, which required a.
a.dostuff();

the reason you get an empty object is because browserify creates an empty object representing module.exports for that module before the module code runs. which means that another module can require it before it's finished, it's just not fully baked.


Related Query

More Query from same tag