thlorenz / browserify-shim

📩 Makes CommonJS incompatible files browserifyable.
MIT License
934 stars 87 forks source link

best practice for using browserify-shim in a dependency of a dependency #219

Closed alex-dow closed 7 years ago

alex-dow commented 7 years ago

I have two projects that both need Twitter Bootstrap. The first project is a library, the second project is a webapp that depends on the library. The problem I'm having is this:

In the Library Project, my package.json has this:

"browserify": {
     "transform": [ [ "browserify-shim" ] ]
},
"browser": {
    "bootstrap": "node_modules/bootstrap/dist/js/bootstrap.js"
}
"browserify-shim": {
    "bootstrap": {
         "exports": "bootstrap",
         "depends": ["jquery:jQuery"]
    }
}

Now, in my App Project, I have the EXACT same settings, but here is where the problem arises. In the App Project, it requires() code from the Library Project, which requires('bootstrap'), but it can't find bootstrap, because browserify-shim is looking in the directory relative to the lib project which would be node_modules/my-library and does not have a node_modules folder since npm doesn't install dependencies of dependencies if they are met by the hosting project.

If I run npm install inside node_modules/my-library everything works as expected. Is there a way to avoid this step? Thanks

bendrucker commented 7 years ago

If I run npm install inside node_modules/my-library everything works as expected. Is there a way to avoid this step?

No, I'd avoid using npm this way for dependencies requiring shimming. As you've discovered, node_modules paths are not guaranteed and hardcoding them breaks. You could consider transforming globally from your app, otherwise you're going to end up with two copies.

alex-dow commented 7 years ago

well I sort of found a solution, which is to include the missing dependency in "bundledDependencies" of the library project's package.json. This seems to force npm to install what I need.