Skip to content

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

Open
browserify/detective
#79

Description

@saeedseyfi

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.

Activity

  1. simonfan commented on Nov 4, 2017

    @simonfan

    @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).

  2. goto-bus-stop commented on Nov 5, 2017

    @goto-bus-stop
    Member

    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.

  3. saeedseyfi commented on Nov 5, 2017

    @saeedseyfi
    Author

    Thanks for your responses @simonfan @goto-bus-stop :) I'll require my source.

  4. saeedseyfi commented on Nov 11, 2017

    @saeedseyfi
    Author

    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?

  5. goto-bus-stop commented on Feb 23, 2018

    @goto-bus-stop
    Member

    browserify/detective#79 will make browserify ignore require calls inside browserify bundles, so it should address this too!

  6. ljharb commented on Feb 25, 2018

    @ljharb
    Member
  7. reopened this on Feb 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions