简体   繁体   中英

Uncaught TypeError: __webpack_require__(...).context is not a function

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

--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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM