11

I tried something like:

var path = '../right/here';
var module = require(path);

but it can't find the module anymore this way, while:

var module = require('../right/here');

works like a charm. Would like to load modules with a generated list of strings, but I can't wrap my head around this problem atm. Any ideas?

5
  • 2
    I do this all the time and it works just fine. when is this going wrong for you? Because if you're using something like webpack or browserify, then yes: this will go very wrong because they can only do static string dependency resolution. Commented Mar 14, 2015 at 17:29
  • oh, browserify is the problem, then(?) Is there an easy way to fix it? Commented Mar 14, 2015 at 17:31
  • 1
    Yes, but you won't like it: hardcode your requirements. For real code that you bundle for production deployment (unlike quick prototyping) there's almost never a need to require from variable, since you'd be writing code that isn't in any way predictable and thus potentially full of bugs. If you need different requirements based on different runs, then build a config object on startup (with process.env or something) and when it comes time to do the requires, use something like vars knox = config.offline ? require("./util/mocks3") : require("knox"); Commented Mar 14, 2015 at 17:36
  • guess that's the best way to do it, then. still gives me some shivers thinking of ~100 modules. thanks for pointing out that it is a browserify problem - pretty obvious, thinking about how it works. will follow your advice. ty. Commented Mar 14, 2015 at 17:55
  • turned that into an answer as well, since having the answer in the comments is not quite as helpful =) Commented Mar 14, 2015 at 18:03

4 Answers 4

6

you can use template to get file dynamically.

var myModule = 'Module1';
var Modules = require(`../path/${myModule}`)
Sign up to request clarification or add additional context in comments.

Comments

4

This is due to how Browserify does its bundling, it can only do static string analysis for requirement rebinding. So, if you want to do browserify bundling, you'll need to hardcode your requirements.

For code that has to go into production deployment (as opposed to quick prototypes, which you rarely ever bother to add bundling for) it's always advisable to stick with static requirements, in part because of the bundling but also because using dynamic strings to give you your requirements means you're writing code that isn't predictable, and can thus potentially be full of bugs you rarely run into and are extremely hard to debug.

If you need different requirements based on different runs (say, dev vs. stage testing vs. production) then it's usually a good idea to use process.env or a config object so that when it comes time to decide which library to require for a specific purposes, you can use something like

var knox = config.offline ? require("./util/mocks3") : require("knox");

That way your code also stays immediately traversable for others who need to track down where something's going wrong, in case a bug does get found.

Comments

2

require('@/path/'.concat(fileName))

Comments

0

You can use .require() to add the files that you want to access calculating its path instead of being static at build time, this way this modules will be included and when calling require() later they will be found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.