首页 > 解决方案 > Webpack 提供的依赖使我的应用程序陷入循环依赖

问题描述

当我在我的 web 应用程序中包含一些外部库并开始使用 Webpack (5.45.1) 构建它时,出现了那个旧的好错误:Cannot read property <..> of undefined. 进入杂草,我发现问题是循环依赖。外部库请求 Node.jsutil一个,随后的依赖关系图如下所示:

./node_modules/util/util.js -> node_modules/console-browserify/index.js -> ./node_modules/assert/build/assert.js -> ./node_modules/assert/../assertion_error.js -> ./node_modules/util/util.js

事实证明,在导出某些内容之前util请求console-browserifyassert需要util导出内容,因此会出现该错误。

而且,原来的util.js包根本不依赖console-browserify,它是通过Webpack通过以下方式注入到那里的:

/* provided dependency */ var console = __webpack_require__(/*! ./node_modules/console-browserify/index.js */ "./node_modules/console-browserify/index.js");

我该如何解决这个问题?为什么 Webpack 会引入这种依赖?

标签: javascriptwebpack

解决方案


推荐阅读