简体   繁体   English

未捕获的类型错误:无法读取 __webpack_require__ 处未定义的属性“调用”

[英]Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__

I am using React.lazy to load some React classes on runtime so that they are not all loaded at once.我正在使用 React.lazy 在运行时加载一些 React 类,这样它们就不会一次全部加载。 My code works for production, but crashes when I am in development mode.我的代码适用于生产,但在我处于开发模式时崩溃。 (UPDATE: My code no longer works in production - see below). (更新:我的代码不再适用于生产 - 见下文)。

The particular error message is very cryptic so hard to know exactly what the issue is:特定的错误消息非常神秘,因此很难确切知道问题是什么:

Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ (main.js:64)

The above error occurred in one of your React components:
    in Unknown
    in Suspense
    in div (created by Main)
    in Main (created by Route)
    in Route (created by App)
    in Switch (created by App)
    in div (created by App)
    in Router (created by BrowserRouter)
    in BrowserRouter (created by App)
    in App

Consider adding an error boundary to your tree to customize error handling behavior.

Uncaught (in promise) TypeError: Cannot read property 'call' of undefined at __webpack_require__ (main.js:64)

Line 64 gives the following code:第 64 行给出了以下代码:

modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

I have other React classes that are not having any issues.我还有其他没有任何问题的 React 类。

The particular class file that I created is called Categories.js.我创建的特定类文件称为 Categories.js。 As far as I know I am not loading the class any differently than any of the ones that are working.据我所知,我加载的类与任何正在工作的类没有任何不同。 I have even tried renaming the class/file and have I have also removed most of my data out of it in case something in the file was causing the issue.我什至尝试过重命名类/文件,并且还删除了其中的大部分数据,以防文件中的某些内容导致问题。

Here are the pertinent lines from my code:以下是我的代码中的相关行:

import React, {Suspense} from 'react';
....
const Categories = React.lazy(()=> import('./Categories'))
....
return (
    <Suspense fallback={<div>Loading...</div>}>
        <Categories class_select={class_select} />
    </Suspense>
 )

If it helps here is my webpack.config.js file:如果有帮助,这是我的 webpack.config.js 文件:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = (env, argv) => {

  const isProduction = (argv.mode === "production")
  return {

          module: {
            rules: [
              {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: "babel-loader",
                  options: {
                    plugins: [
                        "@babel/plugin-syntax-dynamic-import"
                    ]
                  }
                }
              },
              {
                test: /\.html$/,
                use: [
                  {
                    loader: "html-loader"
                  }
                ]
              }
            ]
          },
          ...(isProduction && {
                  optimization: {
                   // minimize: true,
                    minimizer: [
                        new TerserPlugin({
                                terserOptions: {
                                        extractComments: 'all',
                                        compress: {
                                                drop_console: true
                                        },

                                }
                        })
                    ],
                  }
          }),
          devtool: !isProduction && 'eval-source-map',
          plugins: [
            new HtmlWebPackPlugin({
              template: "./src/index.html",
              filename: "./index.html"
            }),
            new CopyPlugin([
              { from: 'src/css', to: 'css' }
            ])
          ]
     };
};

Questions问题

1) What is causing this error? 1)是什么导致了这个错误? 2) Why is it only being caused in dev mode but not production mode? 2)为什么它只在开发模式而不是生产模式引起?

Update更新

My code no longer works in production either.我的代码也不再适用于生产环境。 I am getting the following error:我收到以下错误:

Uncaught (in promise) TypeError: Cannot read property 'call' of undefined at o (main.js:2). 

In fact it is even worse in production than dev.事实上它在生产中甚至比开发更糟糕。 In production none of the React lazy classes are working.在生产中,没有任何 React 惰性类在工作。 In dev it is only one of them that isn't working.在开发中,只有其中一个不起作用。

Process过程

In order to find a potential solution to this issue, I had to tinker with the optimization module, which was indeed the issue here, even when not enabled surprisingly.为了找到这个问题的潜在解决方案,我不得不修改优化模块,这确实是这里的问题,即使没有令人惊讶地启用。 My best guess is that some parameters are set to default in production mode and not in dev mode and this causes the issues of imports and undefined properties.我最好的猜测是一些参数在production模式下设置为默认值而不是在dev模式下,这会导致导入和未定义属性的问题。

I decided to try replicating the deployment environment and check if I could at least "break" the development as well and investigate the issue from here.我决定尝试复制部署环境并检查我是否至少也可以“破坏”开发并从这里调查问题。 These are the parameters that are different between production and development and that were suspect in causing the issue at hand (you can try yourself by switching to the opposite value to put your deployment like the development environment for example).这些是生产和开发之间不同的参数,怀疑是导致手头问题的原因(例如,您可以通过切换到相反的值来尝试将您的deployment放在development环境中)。

On the link I provided in the comment, the user was explaining that the issue was at deployment level and that the way the vendors chunk were built, were colliding with the main chunks and cuts the entry to one another.在我在评论中提供的链接上,用户解释说问题出在部署级别, vendors块的构建方式与main块发生冲突并相互切断entry One of the solution was to use concatenateModules: false apparently, but to no avail, it didn't solve my issue.解决方案之一是使用concatenateModules: false显然,但无济于事,它没有解决我的问题。 So I tried with the others and found the issue bellow.所以我和其他人一起尝试,发现了下面的问题。

Potential solution潜在的解决方案

in module.exports , the optimization object should be editedmodule.exports中,应该编辑optimization对象

optimization: {
    minimize: true,
    namedModules: true,
    namedChunks: true,
    removeAvailableModules: true,
    flagIncludedChunks: true,
    occurrenceOrder: false,
    usedExports: true,
    concatenateModules: true,
    sideEffects: false, // <----- in prod defaults to true if left blank
}

Edit: all of these parameters are set to their opposite between production and development, tweak them at your own leisure, some issues stems from them编辑:所有这些参数在生产和开发之间都设置为相反,您可以根据自己的空闲时间调整它们,一些问题源于它们

Explanation解释

After switching all the parameters I found that the sideEffects one is the one that breaks things and I see why:切换所有参数后,我发现sideEffects是破坏事物的那个,我明白了原因:

The sideEffects flag will break down imports into separate ones as follow, as per the documentation on sideEffects :根据 sideEffects 上的文档, sideEffects 标志会将导入分解为单独的导入:

import { a, b } from "big-module-with-flag"

is rewritten to被重写为

import { a } from "big-module-with-flag/a";
import { b } from "big-module-with-flag/b";

And will try to optimize imports accordingly across the modules, which can cause issues in production.并将尝试相应地优化跨模块的导入,这可能会导致生产问题。 Normally this should help optimizing the size of the package by reducing the bundles, at the cost of removing some imports but can break things at import.通常这应该有助于通过减少包来优化包的大小,代价是删除一些导入,但可能会在导入时破坏东西。

I hope the explanation was somewhat clear, if somebody has deeper knowledge on WebPack optimization, any documentation and enhancement would be welcome.我希望解释清楚一些,如果有人对 WebPack 优化有更深入的了解,欢迎提供任何文档和增强功能。

I also had the same error, extremely difficult to diagnose the root cause from a minified JavaScript stack trace.我也有同样的错误,很难从缩小的 JavaScript 堆栈跟踪中诊断根本原因。 In the end, I just upgraded the Webpack version to the latest 4.X stable release (I was using Angular 11, so no Webpack 5 support) and the problem magically went away.最后,我只是将 Webpack 版本升级到最新的 4.X 稳定版本(我使用的是 Angular 11,所以不支持 Webpack 5),问题神奇地消失了。 Assume there must have been some dependency tree bug in the Webpack 4.2.X version that I was using previously.假设我之前使用的 Webpack 4.2.X 版本中一定存在一些依赖树错误。

Webpack versions reference: https://www.npmjs.com/package/webpack Webpack 版本参考: https ://www.npmjs.com/package/webpack

Commands命令

npm uninstall webpack
npm install --save-dev webpack@4.46.0

I had a similar error that would only occur on production.我有一个类似的错误,只会在生产中发生。 Production had Cloudflare but Test did not.生产有 Cloudflare,但测试没有。 It turned out our Cloudflare cache settings were ignoring our cache busting.原来我们的 Cloudflare 缓存设置忽略了我们的缓存破坏。 Cache busting via query parameters /scripts/main.js?v=1080237476 .通过查询参数清除缓存/scripts/main.js?v=1080237476 So our code-split Webpack output had mis-matched JavaScript files and threw the error Uncaught TypeError: Cannot read property 'call' of undefined .所以我们的代码拆分 Webpack 输出有不匹配的 JavaScript 文件并抛出错误Uncaught TypeError: Cannot read property 'call' of undefined The quick fix was to purge our Cloudflare cache after deploy.快速修复是在部署后清除我们的 Cloudflare 缓存。 Long term fix is to configure our Cloudflare settings or use hashed filenames instead of query parameters for cache busting.长期修复是配置我们的 Cloudflare 设置或使用散列文件名而不是查询参数来清除缓存。

I am using React.lazy to load some React classes on runtime so that they are not all loaded at once.我正在使用 React.lazy 在运行时加载一些 React 类,以便它们不会一次全部加载。 My code works for production, but crashes when I am in development mode.我的代码适用于生产,但在我处于开发模式时崩溃。 (UPDATE: My code no longer works in production - see below). (更新:我的代码不再在生产中工作 - 见下文)。

The particular error message is very cryptic so hard to know exactly what the issue is:特定的错误消息非常神秘,因此很难确切知道问题所在:

Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ (main.js:64)

The above error occurred in one of your React components:
    in Unknown
    in Suspense
    in div (created by Main)
    in Main (created by Route)
    in Route (created by App)
    in Switch (created by App)
    in div (created by App)
    in Router (created by BrowserRouter)
    in BrowserRouter (created by App)
    in App

Consider adding an error boundary to your tree to customize error handling behavior.

Uncaught (in promise) TypeError: Cannot read property 'call' of undefined at __webpack_require__ (main.js:64)

Line 64 gives the following code:第 64 行给出了以下代码:

modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

I have other React classes that are not having any issues.我有其他没有任何问题的 React 类。

The particular class file that I created is called Categories.js.我创建的特定 class 文件称为 Categories.js。 As far as I know I am not loading the class any differently than any of the ones that are working.据我所知,我加载 class 的方式与任何正常工作的方式不同。 I have even tried renaming the class/file and have I have also removed most of my data out of it in case something in the file was causing the issue.我什至尝试重命名类/文件,并且我还删除了大部分数据,以防文件中的某些内容导致问题。

Here are the pertinent lines from my code:以下是我的代码中的相关行:

import React, {Suspense} from 'react';
....
const Categories = React.lazy(()=> import('./Categories'))
....
return (
    <Suspense fallback={<div>Loading...</div>}>
        <Categories class_select={class_select} />
    </Suspense>
 )

If it helps here is my webpack.config.js file:如果这里有帮助的话是我的 webpack.config.js 文件:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = (env, argv) => {

  const isProduction = (argv.mode === "production")
  return {

          module: {
            rules: [
              {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: "babel-loader",
                  options: {
                    plugins: [
                        "@babel/plugin-syntax-dynamic-import"
                    ]
                  }
                }
              },
              {
                test: /\.html$/,
                use: [
                  {
                    loader: "html-loader"
                  }
                ]
              }
            ]
          },
          ...(isProduction && {
                  optimization: {
                   // minimize: true,
                    minimizer: [
                        new TerserPlugin({
                                terserOptions: {
                                        extractComments: 'all',
                                        compress: {
                                                drop_console: true
                                        },

                                }
                        })
                    ],
                  }
          }),
          devtool: !isProduction && 'eval-source-map',
          plugins: [
            new HtmlWebPackPlugin({
              template: "./src/index.html",
              filename: "./index.html"
            }),
            new CopyPlugin([
              { from: 'src/css', to: 'css' }
            ])
          ]
     };
};

Questions问题

1) What is causing this error? 1)是什么导致了这个错误? 2) Why is it only being caused in dev mode but not production mode? 2)为什么它只在开发模式而不是生产模式下引起?

Update更新

My code no longer works in production either.我的代码也不再适用于生产环境。 I am getting the following error:我收到以下错误:

Uncaught (in promise) TypeError: Cannot read property 'call' of undefined at o (main.js:2). 

In fact it is even worse in production than dev.事实上,它在生产环境中甚至比开发环境更糟糕。 In production none of the React lazy classes are working.在生产中,没有一个 React 惰性类在工作。 In dev it is only one of them that isn't working.在开发中,只有其中一个不起作用。

暂无
暂无

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

相关问题 Webpack Uncaught TypeError:无法读取未定义的属性“调用” - Webpack Uncaught TypeError: Cannot read property 'call' of undefined 未捕获的类型错误:__webpack_require__(...).context 不是 function - Uncaught TypeError: __webpack_require__(...).context is not a function 未捕获的类型错误:无法读取未定义的属性“调用” - Uncaught TypeError: Cannot read property 'call' of undefined React - ssh2 模块错误:未捕获的类型错误:__webpack_require__(...).constants 未定义 - React - ssh2 module Error: Uncaught TypeError: __webpack_require__(…).constants is undefined React / Webpack / Django-未捕获的TypeError:无法读取未定义的属性“ XXX” - React/Webpack/Django - Uncaught TypeError: Cannot read property 'XXX' of undefined 未捕获的TypeError:在webpack重建CommonsChunkPlugin后无法读取未定义的属性&#39;call&#39; - Uncaught TypeError: Cannot read property 'call' of undefined after webpack rebuild CommonsChunkPlugin 诊断未捕获的TypeError:__webpack_require __(…).createServer不是函数? - Diagnosing Uncaught TypeError: __webpack_require__(…).createServer is not a function? Karma Jasmine - 未捕获的类型错误:__webpack_require__(...).context 不是 function - Karma Jasmine - Uncaught TypeError: __webpack_require__(...).context is not a function ChartJS:未捕获的TypeError:无法读取未定义的属性'call' - ChartJS: Uncaught TypeError: Cannot read property 'call' of undefined firebaseui.js未捕获TypeError:无法读取未定义的属性'call' - firebaseui.js Uncaught TypeError: Cannot read property 'call' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM