question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I require a file that is built using browserify?

See original GitHub issue

Hello 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 requires 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:open
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
goto-bus-stopcommented, 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.

1reaction
ljharbcommented, Feb 25, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found