webpack - Webpack 构建或运行时错误,在样式加载器上出现“require() is not a function”
问题描述
在使用 webpack 构建 webpack 或运行网站时,我会收到关于 require not be a function 的随机错误。这发生在需要解析的深度嵌套调用堆栈的不同点的不同模块上。
我已经广泛搜索了这个错误,发现它可能是由许多不同的症状引起的。我已经尝试了所有我能找到的方法,但没有一个对我有用。
我正在发布这个解决方案,以显示对我有用的方法,以防其他人可能遇到同样的问题。
以下是 webpack 集的相关部分:
/**
* @returns {import('webpack').RuleSetRule}
*/
module.exports = () => ({
test: /\.js$/,
exclude: /node_modules\/(?!(query-string|strict-uri-encode|modernizr|split-on-first)\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: [
[ '@babel/preset-env', {
// - If useBuiltIns: 'usage' is specified in .babelrc then do not include @babel/polyfill in either webpack.config.js entry array
// nor source. Note, @babel/polyfill still needs to be installed.
// - If useBuiltIns: 'entry' is specified in .babelrc then include @babel/polyfill at the top of the entry point to your application
// via require or import
// - If useBuiltIns key is not specified or it is explicitly set with useBuiltIns: false in your .babelrc, add @babel/polyfill
// directly to the entry array in your webpack.config.js.
useBuiltIns: 'usage',
corejs: '2'
} ]
],
plugins: [
['@babel/plugin-transform-modules-commonjs', {
strictMode: false
}]
]
}
}
})
上面的 babel 加载器给出了以下错误:
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
TypeError: __webpack_require__(...) is not a function
at Object.I8a+ (C:\TRAVLR\Repos\Travlr\Webs\WebApp\node_modules\css-loader\dist\cjs.js!C:\TRAVLR\Repos\Travlr\Webs\WebApp\node_modules\postcss-loader\src\index.js??ref--6-2!C:\TRAVLR\Repos\Travlr\Webs\WebApp\node_modules\sass-loader\lib\loader.js!C:\TRAVLR\Repos\Travlr\Library\UiKit\src\scss\app.scss:580:38)
或者
at eval (webpack:///./node_modules/core-js/modules/_classof.js?:4)
at Object../node_modules/core-js/modules/_classof.js (pkg.core-js.js:889)
at __webpack_require__ (runtime.client.js:782)
at fn (runtime.client.js:150)
at eval (webpack:///./node_modules/core-js/modules/es6.object.to-string.js?:3)
at Object../node_modules/core-js/modules/es6.object.to-string.js (pkg.core-js.js:2167)
at __webpack_require__ (runtime.client.js:782)
at fn (runtime.client.js:150)
at eval (webpack:///./node_modules/core-js/modules/es6.regexp.to-string.js?:11)
at Object../node_modules/core-js/modules/es6.regexp.to-string.js (pkg.core-js.js:2306)```
解决方案
对我有用的是从上面的文件中更改以下行
useBuiltIns: 'usage'
:
useBuiltIns: 'entry'
请注意,这可能对您不起作用。同样的错误似乎有许多不同的原因。
推荐阅读
- react-native - Expo fontello 无法加载
- java - 为 GsmCellLocation 类接口返回的 toString 是什么?
- javascript - 动态获取页面长度选项以使用 Angular 加载数据表
- r - 使用其他动态列名更改具有条件的动态列名
- database - Anylogic数据库集成为BI提供动力?
- pandas-groupby - 删除地图上的错误坐标
- azure - 是否可以安装在独立 Windows VM 上运行的 Azure 云服务(经典)上运行的应用程序?
- javascript - 如何在温度计算器中声明一个变量来解决这个问题?
- office365 - 以编程方式导出/打印到 XPS - WordAPI
- android-studio - 导入项目时出现 Android Studio 错误