You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How can I require a file that is built using browserify? #1773
I built a js library using browserify. Working well in the browser and everything looks great if I put it as a separate file in my html:
<!-- my library -->
<script src="./library/index.js"></script>
<!-- my app that uses the library -->
<script src="./app/build.js"></script>
But when I want to require the library file in my app, browserify complains.
Because the requires are still in the file and if I re-bundle it, browserify wants to re-include the files that are already included.
Now I want to build app/src/core.js the same way ($ browserify app/src/core.js > app/build.js) but it complains: Error: Cannot find module './foo' from '/library'
@saeedseyfi, hmmm would it work for you if you just required your source library/src in your application's code? In case you really need to have the library's code to be separated and be built separately, I would say the best solution would be to publish it as a separate npm package and install it through npm. That way you can require it directly by name in your application's code.
If you don't want to publish the package to npm you could just put it in a git repo and install using npm git (npm install -s git+https://github.com/someuser/some-lib).
Suggestion:
What if browserify changes the requires in the final bundle?!
E.g. require('./foo') to require(7) and 7 points to the part of code that is already bundled. In this scenario if you require the bundle again it only looks for the requires that point to files/modules, not numbers.
Is it practical?
Activity
simonfan commentedon Nov 4, 2017
@saeedseyfi, hmmm would it work for you if you just
required your sourcelibrary/srcin your application's code? In case you really need to have the library's code to be separated and be built separately, I would say the best solution would be to publish it as a separate npm package and install it through npm. That way you canrequireit directly by name in your application's code.If you don't want to publish the package to npm you could just put it in a git repo and install using npm git (
npm install -s git+https://github.com/someuser/some-lib).goto-bus-stop commentedon Nov 5, 2017
yeah if possible, requiring the source files directly is ideal. if you must require a browserify bundle, you can first pass it through https://www.npmjs.com/package/derequire to rewrite the
require()calls.saeedseyfi commentedon Nov 5, 2017
Thanks for your responses @simonfan @goto-bus-stop :) I'll
requiremy source.saeedseyfi commentedon Nov 11, 2017
Suggestion:
What if browserify changes the
requires in the final bundle?!E.g.
require('./foo')torequire(7)and7points to the part of code that is already bundled. In this scenario if yourequirethe bundle again it only looks for therequires that point to files/modules, not numbers.Is it practical?
goto-bus-stop commentedon Feb 23, 2018
browserify/detective#79 will make browserify ignore
requirecalls inside browserify bundles, so it should address this too!ljharb commentedon Feb 25, 2018
Pending browserify/detective#79