1

I want to do something like:

var dynamicRequire = require.context('./', true);
console.log(dynamicRequire.keys());
dynamicRequire('react/foo/bar');

But the console.log only shows files from the local directory, not the npm packages. When webpack builds i can see it get included as number 234 but the mapping from that path to that number is lost. How can I accomplish this? Thanks!

2
  • You need to make lazy loading of some modules or you need to load module from variable name? Commented Jan 24, 2016 at 18:29
  • Yeah, I want to load react dynamically from require Commented Jan 24, 2016 at 18:32

1 Answer 1

1

I'm not sure, what your problem exactly is here, but I suspect, that for your purpose you have just forgotten to provide require.context with second argument, which is a flag, that decides, whether the webpack should look into your subfolders and pick your files there also. So you could use require.context('./', true, [some regexp maybe ?]).

Let me know if that's not addressing your problem.

Sign up to request clarification or add additional context in comments.

6 Comments

This does not let me require react.
What are you trying to achieve anyway? Why do you want to require react on demand? Seems unnecessary to me.
You cant have more than one react instance loaded at a time, therefore multiple modules have to share the same instance.
And where is the problem with that? Just curious :). In any case, React doesn't recommend use of multiple instances, have a look at React's helpful warnings
The drag and drop react library depends on there only being one instance and fails pretty hard if thats not true. In any case, you're right that React recommends against it, which is why I am trying to make multiple modules use the same instance of react instead of including it multiple times.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.