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
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?
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.
+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?
@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?
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.)
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.
@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).
@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.
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.
@ajkochanowicz At least as a temporary workaround, can you use opts.noParse? So using @jackcviers' scenario, browserify module-a as standalone. In module-brequire('module-a'), and browserify module-b like so:
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.
Activity
zpao commentedon Jun 4, 2013
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
requirestatement 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.jackcviers commentedon Jun 7, 2013
Right. I adjusted by making most deps independent libs. Just not my preferred course of action.
ForbesLindesay commentedon Nov 5, 2013
Another option is to minify the result. That way all the
requirefunctions would get renamed, so they would be ignored by browserify.ahuth commentedon May 31, 2014
+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
requirestatements when bundling work, or is there something else I should be looking at?ahuth commentedon Jun 1, 2014
@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?
ForbesLindesay commentedon Jun 1, 2014
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.)
ahuth commentedon Jun 1, 2014
Thank you, @ForbesLindesay.
jperkelens commentedon Aug 20, 2014
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.
zpao commentedon Aug 20, 2014
@jperkelens the original bundle needs to have been made with compatible options.
--standalonestopped usingderequirein 5.x I think, so it's a separate transform now (or maybe just a separate step entirely).jperkelens commentedon Aug 20, 2014
@zpao Thanks a lot. This totally cleared up my problem.
adamjgrant commentedon Sep 2, 2014
@zpao @jperkelens So what exactly needs to be done to solve this?
jperkelens commentedon Sep 2, 2014
@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.
adamjgrant commentedon Sep 2, 2014
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.
jmm commentedon Jan 27, 2015
@ajkochanowicz At least as a temporary workaround, can you use
opts.noParse? So using @jackcviers' scenario, browserifymodule-aas standalone. Inmodule-brequire('module-a'), and browserifymodule-blike so:The
noParsevalues 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 yourequire()module-athough -- there are currently a lot of issues with resolving and matching up paths specified in various places.tcql commentedon Apr 17, 2015
@jmm thanks for the
noParsesuggestion! this helped me clear up an issue with a third-party module that was not built standalonejmm commentedon Apr 17, 2015
@tchannel Cool, thanks!
FYI everybody on this thread, how to solve the "browserify a browserified module" case is being discussed in #1151.
Fix Browserify nonsense
Exports uglified version in dist
Allow Uglify to mangle names to prevent this issue: browserify/browse…
De-require browserified script to allow browserifying it again