Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic requires don't find files in the browser #377

Closed
btakita opened this issue Apr 11, 2013 · 15 comments
Closed

dynamic requires don't find files in the browser #377

btakita opened this issue Apr 11, 2013 · 15 comments

Comments

@btakita
Copy link

btakita commented Apr 11, 2013

I'm trying to require a file dynamically.

<section data-view="views/root" id="root"></section>

I have a parse method which parses the DOM for data-view and requires that file.

function parse() {
  _.each($('[data-view]'), function(el) {
    var $el = $(el);
    require('./' + $el.data('view'));
  })
}

I then get the following error in the browser "Error: Cannot find module './views/root'". It works when I execute the code in node.js.

@ghost
Copy link

ghost commented Apr 11, 2013

Browserify can only analyze static requires. It is not in the scope of browserify to handle dynamic requires.

@ghost ghost closed this as completed Apr 11, 2013
@michaelficarra
Copy link

You should still get a compile-time error rather than a runtime error.

@sokra
Copy link

sokra commented Apr 11, 2013

You can try webpack, which can handle this kind of require: example

@jackcviers
Copy link

@substack Thanks for the answer. That's what I assumed, and now have moved to using browserify only in the apps that depend on the libraries. For future reference, when I have questions like this, do you prefer issues or is there an irc room or message board? I assume that you don't want me to contact you via Twitter or email.

@mjlescano
Copy link

Just as a reference, got around with this with a hacky solution: http://stackoverflow.com/questions/21642398/compiling-dynamically-required-modules-with-browserify/27672458#27672458

@therebelrobot
Copy link

hey @substack, is there a specific reason this isn't in scope? I mean, it's a fairly easy use case to have an array of items, and do a simple forEach to require them in... is there something under the hood that is breaking on this, and if so, can I take a look at it for you?

@ghost
Copy link

ghost commented Mar 20, 2015

@therebelrobot you can use a transform like bulkify to do that.

@kyle-dorman
Copy link

I'm trying to use the below umd syntax which uses a dynamic require statement:

// app/CachingStore
(function (define) {

// dependencies are listed in the dependency array
define(['./store', 'meld'], function (store, meld) {
"use strict";
  var cache = {};

    // create the module
    meld.around(store, 'get', function (jp) {
        var key = jp.args.join('|');
        return key in cache ? cache[key] : cache[key] = jp.proceed();
    };

    // return your module's exports
    return store;
});

}(
    typeof define == 'function' && define.amd
        ? define
        : function (ids, factory) {
            // note: the lambda function cannot be removed in some CJS environments
            var deps = ids.map(function (id) { return require(id); });
            module.exports = factory.apply(null, deps);
        }
));

Do you know if I van use bulkify to do this?

@ghost
Copy link

ghost commented Mar 20, 2015

@kyle-dorman bulkify doesn't understand AMD. You can only give it globs of paths to require.

@kyle-dorman
Copy link

Ah ya that makes sense. I think I framed my question incorrectly.

This form of UMD uses dynamic require dependancies for CommonJS syntax.

var ids = ['./store', 'meld'];
var deps = ids.map(function (id) { return require(id); });
module.exports = factory.apply(null, deps);

I know this is not supported by Browserify but are you aware of any existing modules that can determine module dependancies for dynamic require statements?

@x5engine

This comment has been minimized.

@ljharb

This comment has been minimized.

@x5engine

This comment has been minimized.

@ljharb

This comment has been minimized.

@x5engine

This comment has been minimized.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

9 participants