Skip to content

Browserify a Browserified module #374

Closed

Description

@jackcviers

So I have three modules to use in an app in a browser.

module-b depends on module-a.

module-a depends on Handlebars and is composed of several individual internal dependencies, relatively required as in the documentation. module-a's foo.js requires bar.js, like so:

var Handlebars = require('handlebars');
var bar = require('./bar.js');

I browserify module-a. I run it through tests, and everything is good and index.js is produced and it contains foo, bar, and Handlebars and the exports look good and are tested via the requires in the test, so, I npm link module-a.

I go to module-b and npm link module-a, which installs a symlink in the node_modules folder of module-b.

When I browserify module-b, which has a statement of var module-a = require('module-a');, I get a bunch of browserify errors, like the following:

Error: module "./handlebars/base" not found from "/home/blah/Development/module-b/node_modules/module-a/index.js"

This seems strange, since module-a is already browserified. I've tried with -r module-a, -x module-a, --ignore-missing, -i 'handlebars'. Browserify just keeps spitting out the same error. I assume that it would fail for module-a's internal ./bar.js dependency as well, but it dies before it even gets that far.

Is this expected behavior, is it a bug, or am I not configuring the cli call to browserify correctly?

Activity

  1. zpao commented on Jun 4, 2013

    @zpao

    FWIW, I hit this with React and gave up. I think it's expected but just feels broken. I'm fairly sure this is because browserify is visiting every require statement and trying to transform that. But it's doing it without knowledge that it's already looking at browserified code. It only looks in the node module paths, so when it hits require('./handlebars/baase'), it's looking in module-b's paths.

  2. jackcviers commented on Jun 7, 2013

    @jackcviers
    Author

    Right. I adjusted by making most deps independent libs. Just not my preferred course of action.

  3. ForbesLindesay commented on Nov 5, 2013

    @ForbesLindesay
    Contributor

    Another option is to minify the result. That way all the require functions would get renamed, so they would be ignored by browserify.

  4. ahuth commented on May 31, 2014

    @ahuth

    +1 for re-opening. Browserify is obviously a very viable way of packaging libraries/modules/components for distribution, and we should be able to bundle these in our own projects that use Browserify.

    Before I do a pull request, what's the best way forward with this? Would aliasing require statements when bundling work, or is there something else I should be looking at?

  5. ahuth commented on Jun 1, 2014

    @ahuth

    @substack Do you mean that the module I want to use should be built with the standalone option, or that I should use the option when trying to use the already browserified module myself?

  6. ForbesLindesay commented on Jun 1, 2014

    @ForbesLindesay
    Contributor

    The module you want to use should be built with the standalone option. That option is intended for creating a file that can be loaded by other module systems (including browserify itself, node.js, requirejs, etc.)

  7. ahuth commented on Jun 1, 2014

    @ahuth

    Thank you, @ForbesLindesay.

  8. jperkelens commented on Aug 20, 2014

    @jperkelens

    In what version of Browserify does this work? I'm using 4.2.1 and am still getting "cannot find module errors" when trying to browserify a bundle built with standalone.

  9. zpao commented on Aug 20, 2014

    @zpao

    @jperkelens the original bundle needs to have been made with compatible options. --standalone stopped using derequire in 5.x I think, so it's a separate transform now (or maybe just a separate step entirely).

  10. jperkelens commented on Aug 20, 2014

    @jperkelens

    @zpao Thanks a lot. This totally cleared up my problem.

  11. adamjgrant commented on Sep 2, 2014

    @adamjgrant

    @zpao @jperkelens So what exactly needs to be done to solve this?

  12. jperkelens commented on Sep 2, 2014

    @jperkelens

    @ajkochanowicz I ran my dependencies through derequire before bundling it, but I own the dependency. If you're having that problem with a third party, not sure what the fix is.

  13. adamjgrant commented on Sep 2, 2014

    @adamjgrant

    Thanks, @jperkelens. I'm still trying to figure out how to change a package's dependencies in this way. Doing a lot of Googling and not finding the answer.

  14. jmm commented on Jan 27, 2015

    @jmm
    Collaborator

    @ajkochanowicz At least as a temporary workaround, can you use opts.noParse? So using @jackcviers' scenario, browserify module-a as standalone. In module-b require('module-a'), and browserify module-b like so:

    browserify({
      entries: ['./entry'],
      noParse: ['/abs/path/to/module-a-including-extension.js'],
    )

    The noParse values currently appear to be sensitive to how the path is indicated. In my quick testing, the absolute path with extension was the ticket. It may depend how you require() module-a though -- there are currently a lot of issues with resolving and matching up paths specified in various places.

  15. tcql commented on Apr 17, 2015

    @tcql

    @jmm thanks for the noParse suggestion! this helped me clear up an issue with a third-party module that was not built standalone

  16. jmm commented on Apr 17, 2015

    @jmm
    Collaborator

    @tchannel Cool, thanks!

    FYI everybody on this thread, how to solve the "browserify a browserified module" case is being discussed in #1151.

  17. added a commit that references this issue on Oct 21, 2015
  18. added a commit that references this issue on Mar 1, 2016
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