Reputation: 55
When I try to dynamically import xml files using this code:
const path = require.context(`../report/${runId}/`, false, /\.xml$/);
I am getting this error:
Uncaught (in promise) TypeError: __webpack_require__(...).context is not a function
at fetchData (xmlparser.js:28:1)
at Run.getTests (Run.js:16:1)
at Run.componentDidMount (Run.js:10:1)
at commitLayoutEffectOnFiber (react-dom.development.js:23305:1)
at commitLayoutMountEffects_complete (react-dom.development.js:24688:1)
at commitLayoutEffects_begin (react-dom.development.js:24674:1)
at commitLayoutEffects (react-dom.development.js:24612:1)
at commitRootImpl (react-dom.development.js:26823:1)
at commitRoot (react-dom.development.js:26682:1)
at performSyncWorkOnRoot (react-dom.development.js:26117:1)
But as soon as I import the XML files as following it works:
const path = require.context(`../report/1/`, false, /\.xml$/);
Note - runId is declared as 1, so it isn't undefined
Upvotes: 2
Views: 1666
Reputation: 235
--edit:
my answer is wrong. stackoverflow.com/questions/54059179/what-is-require-context require.context is evaluated at compile time, so you must pass a static string, not a variable or dynamic concatenation, evaluated runtime. This is why the second example works.
Upvotes: 1