I am facing a strange problem running latest browserify (10.2.4) and gulp. It seems that the same file is being resolved multiple times, and so I don't get the same instance when creating a singleton. I've put a 'debugger' before module.exports and verified it is called twice.
note #1: I have a guess that says that browserify caches the files based on the string, and so when calling relative paths from different places will not have the same string, even though they point to the same place.
note #2: I was using Browserify 3~ until now, and only when I upgraded it started happening.
Example:
Given:
app.js
/folder1/foo.js
/folder2/bar.js
/folder1/foo.js:
function Foo(){}
module.exports = new Foo(); // Creating a singleton
/folder2/bar.js
var foo = require('../folder1/foo');
function Bar(){
// do something with foo
}
module.exports = Bar;
/app.js
var foo = require('./folder1/foo'),
Bar = require('./folder2/bar');
var bar = new Bar();
// Now 'foo' here and 'foo' inside Bar are not the same instance.