webpack4 style-loader less-loader 出现 TypeError: this.getOptions is not a function at Object.loader

在配置webpack时遇到CSS打包错误,具体表现为Module build failed,错误源在于style-loader版本过高。通过降低style-loader到^2.0.0以及css-loader到^5.2.7解决了问题。对于使用less的情况,less-loader应为^7.3.0。这是一个关于webpack配置和依赖管理的常见问题解决案例。
摘要由CSDN通过智能技术生成

今天配置了一下 webpack.config.js , 然而css的打包一直出错,配置都是常规配置,错误如下:

ERROR in ./src/search.css
Module build failed (from ./node_modules/style-loader/dist/cjs.js):
TypeError: this.getOptions is not a function
    at Object.loader (C:\Users\xingmu\ws\practice\webpack-gk\01\node_modules\style-loader\dist\index.js:19:24)
 @ ./src/search.js 26:0-22
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! 01@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the 01@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xingmu\AppData\Roaming\npm-cache\_logs\2021-07-19T15_59_55_660Z-debug.log

Process finished with exit code 2

经过排查测试,发下是由于style-loader的版本过高导致的,记录一下:

webpack4 可用的 css 文件的 loader 对应最高版本如下:

"css-loader": "^5.2.7",
"style-loader": "^2.0.0",

同时,如果使用的是 lesswebpack4 对应的 less-loader 可用版本配置如下:

"css-loader": "^5.2.7",
"style-loader": "^2.0.0",
"less-loader": "^7.3.0",
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值