browserify: dynamic requires don't find files in the browser

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.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 1
  • Comments: 15 (2 by maintainers)

Most upvoted comments

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

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?