【问题标题】:TypeError: __webpack_require__.i(...) is not a functionTypeError: __webpack_require__.i(...) 不是函数
【发布时间】:2017-04-13 21:42:43
【问题描述】:

我在尝试简化导入时遇到 webpack TypeError。以下代码可以正常工作。在这里,我从 core/components/form/index.js 导入一个名为 smartForm 的 React 高阶组件 (HOC)。

core/components/form/index.js(对smartForm进行命名导出)

export smartForm from './smart-form';

login-form.jsx(导入并使用smartForm

import { smartForm } from 'core/components/form';
class LoginForm extends React.Component {
    ...
}
export default smartForm(LoginForm);

但是,我想将导入简化为 import { smartForm } from 'core'。所以我在core/index.js 中重新导出了smart-form 并从core 导入它。请看下面的代码:

core/index.js(对smartForm进行命名导出)

export { smartForm } from './components/form';
// export smartForm from './components/form';  <--- Also tried this

login-form.jsx(导入并使用smartForm

import { smartForm } from 'core';
class LoginForm extends React.Component {
    ...
}
export default smartForm(LoginForm); // <--- Runtime exception here 

此代码编译没有任何问题,但我在export default smartForm(LoginForm); 行收到以下运行时异常:

login-form.jsx:83 Uncaught TypeError: webpack_require.i(...) is not a function(...)

我错过了什么?

附:以下是我正在使用的 Bable 和插件版本:

"babel-core": "^6.18.2",
"babel-preset-es2015-webpack": "^6.4.3",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-1": "^6.16.0",

【问题讨论】:

    标签: reactjs webpack ecmascript-6 babeljs es6-module-loader


    【解决方案1】:

    tl;博士

    • 对于提问者:将此添加到您的webpack.config.js

      resolve: {
        alias: {
          core: path.join(__dirname, 'core'),
        },
      },
      
    • 对于一般观众:确保您尝试导入的内容确实存在于该包中。

    说明

    提问者的问题:像 npm 模块一样导入自己的代码

    您尝试以与从 node_modules 文件夹中的 npm 包中导入某些内容相同的方式导入模块的导出:import { something } from 'packagename';。问题在于它不能开箱即用。 Node.js docs 给出了原因的答案:

    如果没有前导“/”、“./”或“../”来表示文件,则模块必须是核心模块或从node_modules 文件夹加载。

    因此,您要么必须按照 Waldo JeffersSpain Train 的建议并编写 import { smartForm } from './core',要么您可以配置 webpack,以便它可以通过 its aliases 解析您的导入路径,这些路径是为解决这个确切问题而创建的。

    一般调试错误信息

    如果您尝试从现有 npm 包(在 node_modules 中)导入某些内容,但 导出的内容不存在,则可能会出现此错误。在这种情况下,确保没有拼写错误,并且您尝试导入的给定内容确实在该包中。现在流行将库分解为多个 npm 包,您可能试图从错误的包中导入

    所以如果你得到这样的东西:

    TypeError: __webpack_require__.i(...) is not a function  
       at /home/user/code/test/index.js:165080:81  
       at Layer.handle [as handle_request] (/home/user/code/test/index.js:49645:5)
    

    要获取有关应检查的导入的更多信息,只需打开 webpack 生成的输出文件并转到错误堆栈中最上面一行标记的行(在本例中为 165080)。您应该看到类似:__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_9_react_router_dom__["match"])。这告诉我们react-router-dom 中没有match 导出(或者有但不是函数),因此我们需要检查该导入周围的东西。

    【讨论】:

      【解决方案2】:

      由于core 是您的应用程序的文件夹而不是 npm 模块,Webpack 无法理解您要加载哪个文件。您必须使用指向文件的正确路径。您必须将import { smartForm } from 'core'; 替换为import { smartForm } from './core/index.js

      【讨论】:

      【解决方案3】:

      发生此错误的原因有很多。一旦我在file-loader 中添加js 时遇到此错误,然后当我删除它时它开始正常工作。

       {
           test: /\.(ttf|eot|svg|gif|jpg|png|js)(\?[\s\S]+)?$/,
           use: 'file-loader'
       },
      

      当我删除 |js 时,它可以工作

       {
           test: /\.(ttf|eot|svg|gif|jpg|png)(\?[\s\S]+)?$/,
           use: 'file-loader'
       },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-23
        • 2021-12-10
        • 2022-08-23
        • 2020-12-23
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多