chacham15
chacham15

Reputation: 14251

How can I dynamically require an npm dependency file loaded with webpack?

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!

Upvotes: 1

Views: 199

Answers (1)

Daniel Macak
Daniel Macak

Reputation: 17083

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.

Upvotes: 1

Related Questions