question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamic requires don't find files in the browser

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
sokracommented, Apr 11, 2013

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

1reaction
therebelrobotcommented, Mar 19, 2015

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browserify require file with dynamic path fails, but works when ...
If I don't require the file using an hard-coded path, it fails. I don't explain why since the hardcoded path and the dynamic...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
Troubleshoot viewing PDF files on the web - Adobe Support
Try viewing a different PDF. For example, see if this sample form displays in your browser. If Acrobat or Reader can open the...
Read more >
Microsoft Edge Browser Policy Documentation
If you don't configure this policy, Microsoft Edge Application Guard uses the ... Dynamic Policy Refresh: No - Requires browser restart ...
Read more >
Dependency resolution - Parcel
Dependencies describe where to find the file containing the code you rely on, ... For example, in JavaScript the import statement or require...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found