首页 > 解决方案 > Webpack 包错误:无法读取未定义的属性

问题描述

我的网络应用程序使用 Webpack 5.45.1 作为模块捆绑器和唯一的库 - ethereum web3.js1.4.0。

// webpack configuration file
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const path = require('path');

module.exports = {
    mode: 'development',
    output: {
        path: path.resolve(__dirname, 'dist/dev'),
        filename: '[name].bundle.js',
    },
    entry: {
        index: './src/index.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['css-loader'],
            }
        ]
    },
    plugins: [
        new NodePolyfillPlugin()
    ]
}

入口点包含一个语句:import Web3 from 'web3';

使用浏览器执行生成的包会出现以下错误:

assertion_error.js:486 Uncaught TypeError: Cannot read property 'custom' of undefined
    at eval (assertion_error.js:486)
    at eval (assertion_error.js:500)
    at Object../node_modules/assert/build/internal/assert/assertion_error.js (index.bundle.js:1063)
    at __webpack_require__ (index.bundle.js:6721)
    at eval (assert.js:38)
    at Object../node_modules/assert/build/assert.js (index.bundle.js:1052)
    at __webpack_require__ (index.bundle.js:6721)
    at eval (index.js:3)
    at Object../node_modules/console-browserify/index.js (index.bundle.js:1624)
    at __webpack_require__ (index.bundle.js:6721)

我发现了,undefined是这个表达式的一个值:require('util/').inspect.

任何人都可以提供有关如何修复它的提示吗?

标签: javascriptwebpack

解决方案


事实证明,在我的情况下,问题出在 中NodePolyfillPlugin,它有一个与循环依赖相关的错误。我在github上打开了这个问题。


推荐阅读