dynamic requires don't find files in the browser
See original GitHub issueI’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:
- Created 10 years ago
- Reactions:1
- Comments:15 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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?