简体   繁体   English

未捕获的类型错误:__webpack_require__(...).context 不是 function

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

When I try to dynamically import xml files using this code:当我尝试使用此代码动态导入 xml 文件时:

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:但是一旦我导入 XML 文件,它就可以工作了:

const path = require.context(`../report/1/`, false, /\.xml$/);

Note - runId is declared as 1, so it isn't undefined注意 - runId 被声明为 1,所以它不是未定义的

--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. stackoverflow.com/questions/54059179/what-is-require-context require.context 在编译时评估,因此您必须传递一个 static 字符串,而不是变量或动态连接,评估运行时。 This is why the second example works.这就是第二个示例有效的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 __webpack_require__(...).context 不是 function - __webpack_require__(...).context is not a function 诊断未捕获的TypeError:__webpack_require __(…).createServer不是函数? - Diagnosing Uncaught TypeError: __webpack_require__(…).createServer is not a function? TypeError: __webpack_require__(...).context 不是 function - 硬编码但不带参数 - TypeError: __webpack_require__(…).context is not a function - Works hardcoded but not with parameter 未捕获的类型错误:无法读取 __webpack_require__ 处未定义的属性“调用” - Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ React - ssh2 模块错误:未捕获的类型错误:__webpack_require__(...).constants 未定义 - React - ssh2 module Error: Uncaught TypeError: __webpack_require__(…).constants is undefined 未捕获的类型错误:__webpack_require__.r 不是函数 - Uncaught TypeError: __webpack_require__.r is not a function 未捕获的类型错误:__webpack_require__.e 不是 function - Uncaught TypeError: __webpack_require__.e is not a function 为什么webpack向'__webpack_require__'函数添加属性? - Why webpack add properties to '__webpack_require__' function? 在 __webpack_require__ 中的 NextJS 中构建错误 - Build errors in NextJS in __webpack_require__ 未在反射元数据上定义需求-__webpack_require__问题 - Require is not defined on reflect-metadata - __webpack_require__ issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM