How can I require a file that is built using browserify?
See original GitHub issueHello browserify lords 😃
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 require
s are still in the file and if I re-bundle it, browserify wants to re-include the files that are already included.
My files:
- library
- src
- core.js
- foo.js
- build.js
- app
- src
- core.js
- build.js
library/src/foo.js
:
module.exports = 'foo';
library/src/core.js
:
console.log(require('./foo'));
app/src/core.js
:
require('../../library/build');
library/build.js
is created using browserify:
$ browserify library/src/core.js > library/build.js
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'
I created a test repo to demo the issue: https://github.com/saeedseyfi/browserify-issue
Please advise.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Browserify Tutorial - Using require() In The Browser - YouTube
Learn how to use the require () function to import Javascript modules in the front-end part of your web application ( in the...
Read more >javascript - How to require a built file using browserify? - Stack ...
I built a js library using browserify. Working well in the browser and everything looks great if I put it as a separate...
Read more >Browserify
Bundle up your first module ; Install the uniq module with npm: ; Now recursively bundle up all the required modules starting at...
Read more >Using Browserify to require modules in the browser, just like in ...
Listing 3 HTML file for our simple Browserify example Now, if you save all that and run npm run build-my-js, Browserify will compile...
Read more >Getting Started with Browserify - SitePoint
You include your required JavaScript files using require('./yourfancyJSfile.js') statements and can also import publicly available modules from ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Pending https://github.com/browserify/detective/pull/79