TypeError: __webpack_require__(...).context is not a function - Works hardcoded but not with parameter

Viewed 1384
const importAll = (r) => r.keys().map(r);
const importAllFrom = (dir) => importAll(require.context(dir, false, /\.(png|jpe?g|svg)$/))

Calling `importAllFrom("../images") throws an error 'webpack_require__(...).context is not a function'.

However the following code where I hardcode the directory name works just fine.

const importAll = (r) => r.keys().map(r);
const importAllFrom = (dir) => importAll(require.context("../images", false, /\.(png|jpe?g|svg)$/))

All the code is in the same react component.

Am I missing something really silly??

1 Answers

It didn't actually cross my mind to check the docs for this problem because 99% of the time this sort of problem is a typo but actually they state that this is how it's supposed to work:

The arguments passed to require.context must be literals!

Related