Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browserify resolves the same file multiple times #1315

Open
shlomokraus opened this issue Jun 29, 2015 · 4 comments
Open

Browserify resolves the same file multiple times #1315

shlomokraus opened this issue Jun 29, 2015 · 4 comments

Comments

@shlomokraus
Copy link

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.
@chrisirhc
Copy link
Contributor

browserify will load the same file twice even if the contents are exactly the same due to its dedupe behavior. See my comment here: #1063 (comment)

@shlomokraus
Copy link
Author

If that so, how can I create Singletons the correct way??

@giggio
Copy link

giggio commented Jul 18, 2015

I have found that browserify-resolutions solves the problem. I had the same problem with jquery plugins, I had one instance for each jquery called by each plugin. browserify-resolutions fixed it.

@yungsnug
Copy link

How do I use browserify-resolutions? I have npm installed it but I don't know where the code should actually go. I'm using the Sails framework and I have a 'browserify.js' file, but I still don't know where to put browserify-resolutions. I'm very new to all of this and I'm getting the same "Invariant..." error. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants