webpack踩坑記錄

無論學(xué)什么都會(huì)一步一個(gè)錯(cuò),在此記錄每一步錯(cuò)誤供參考。

1、ReferenceError: _dirname is not defined

運(yùn)行截圖

原因:__dirname 注意這里是兩個(gè)下劃線
解決方案為:將webpack.dev.config.js中的_dirname 改為 __dirname 注意這里是兩個(gè)下劃線
參照:https://www.cnblogs.com/dqcer/p/9293508.html

2、Tapable.plugin is deprecated. Use new API on .hooks instead

原因:extract-text-webpack-plugin目前版本不支持webpack4
解決方案:使用extract-text-webpack-plugin的最新的beta版

npm install extract-text-webpack-plugin@next

參照:https://blog.csdn.net/u011215669/article/details/81269386

3、Module not found: Error: Can't resolve 'webpack/hot' in 'xxxxxxxxxxx'

運(yùn)行截圖

原因:未知
解決方案:在本項(xiàng)目中重新安裝webpack:npm install webpack(不加-g)
參照:https://segmentfault.com/q/1010000009511630

4、Error: Cannot find module 'webpack/lib/optimize/CommonsChunkPlugin'

運(yùn)行截圖

原因:webpack4 移除了 CommonsChunkPlugin,所以需要作相應(yīng)的修改。
解決方案:在webpack.config.js將

module.exports = {
    plugins: [
//注釋掉這一段
//     new webpack.optimize.CommonsChunkPlugin({
//       name: 'common' // 指定公共 bundle 的名稱。
//     })
    ],
//增加這個(gè)配置項(xiàng),與plugins同級(jí)
   optimization: {
     splitChunks: {
       name: 'common'
     }
   },

5、FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

原因:nodejs內(nèi)存太大,溢出
解決方案:
package打包命令增加--max_old_space_size=2048

node --max_old_space_size=2048 build/build.js release

參照:https://segmentfault.com/q/1010000006108492

6、webpack配置都沒問題,但是打包的時(shí)候打包不出來

原因1: webpack的根目錄是命令運(yùn)行的目錄。
解決方案1:更改entry入口地址,與項(xiàng)目根目錄對(duì)齊。
直接用webpack --config xxxx(配置),會(huì)直接暴露webpack報(bào)錯(cuò)。

7、Error/ No PostCSS Config found in

解決方案:在項(xiàng)目根目錄創(chuàng)建 postcss.config.js 文件,并配置以下內(nèi)容

module.exports = {  
  plugins: {  
    'autoprefixer': {browsers: 'last 5 version'}  
  }  
} 

參照:https://yq.aliyun.com/articles/646385

8、Window is not defined with css loader

運(yùn)行截圖

原因以及解決方案:因?yàn)閟tyle-loader和 mini-css-extract-plugin 沖突,去除掉style-loader即可

9、Error: Path variable [contenthash:8] not implemented in this context: [name]_[contenthash:8].css

運(yùn)行截圖

解決方案:
utils.js
刪掉 ExtractTextPlugin,改用 MiniCssExtractPlugin

if (options.extract) {
  const extractLoader = {
    loader: MiniCssExtractPlugin.loader,
    options: {}
  }
  return [extractLoader, 'css-loader'].concat(['postcss-loader'], loaders);
} else {
  return ['vue-style-loader', 'css-loader'].concat(['postcss-loader'], loaders);
}

10、 Error: No PostCSS Config found...

解決方案:在項(xiàng)目根目錄新建postcss.config.js文件,并對(duì)postcss進(jìn)行配置:

module.exports = {  
  plugins: {  
    'autoprefixer': {browsers: 'last 5 version'}  
  }  
} 

11、TypeError: Cannot read property 'compilation' of undefined或者TypeError: Cannot read property 'emit' of undefined

運(yùn)行截圖1

運(yùn)行截圖2

原因:webpack3需要將組件降級(jí)
解決方案:

npm install uglifyjs-webpack-plugin@1.0.0 optimize-css-assets-webpack-plugin@2 copy-webpack-plu
gin@4 --save-dev

12、ERROR in Error: Child compilation failed:

運(yùn)行截圖

原因:index.html和

        new HtmlWebpackPlugin({
            filename: config.build.index,
            template: 'index.html',
            inject: true,
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true
            },
            chunksSortMode: 'dependency'
        })

中的index是否對(duì)的上,本人是忘記添加index.html
解決方案: 增加相應(yīng)位置的index.html或者其他類型模板

13、
運(yùn)行截圖

原因: webpack-dev-server和webpack版本對(duì)不上,webpack3使用@2的版本
解決方案:

npm i webpack-dev-server@2 -D

14、Invalid CSS after "...load the styles": expected 1 selector or at -rule, was "var content = requi"

* ./assets/styles/scss/themes/red/index.scss in ./src/main.ts

image.png

原因:rules中規(guī)則配置可能重復(fù)了;
解決方案:要么去掉原來樣式相關(guān)的loader或者不要添加自己的樣式相關(guān)的loader
參照:https://segmentfault.com/a/1190000013196997

15、Module build failed: ReferenceError: document is not defined

image.png

原因:暫時(shí)無法解釋

解決:
解決

使用fallback就不報(bào)這個(gè)錯(cuò)誤了

16、vue項(xiàng)目配置 autoprefixer 報(bào)出警告問題

image.png

解決方案:將postcss.config.js中的browsers改成overrideBrowserslist

//修改前
module.exports = {
    plugins: {
        'autoprefixer': {browsers: 'last 5 version'}
    }
};

//修改后
module.exports = {
    plugins: {
        'autoprefixer': {overrideBrowserslist: 'last 5 version'}
    }
};

17、webpack vue-loader was used without the corresponding plugin. Make sure to include V..

解決方案: 在webpack配置中增加

const VueLoaderPlugin = require("vue-loader/lib/plugin");
 plugins: [
        new VueLoaderPlugin() //vue的樣式插件
    ]

18、TypeError: CleanWebpackPlugin is not a constructor

const { CleanWebpackPlugin } = require('clean-webpack-plugin'); //新版更換為這種
const webpackConfig = {
    plugins: [
        new CleanWebpackPlugin(),
    ],
};
module.exports = webpackConfig;

19、Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead

解決辦法:

npm install –save-dev extract-text-webpack-plugin@next
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 寫在前面的話 閱讀本文之前,先看下面這個(gè)webpack的配置文件,如果每一項(xiàng)你都懂,那本文能帶給你的收獲也許就比較...
    不忘初心_9a16閱讀 3,326評(píng)論 0 16
  • GitChat技術(shù)雜談 前言 本文較長,為了節(jié)省你的閱讀時(shí)間,在文前列寫作思路如下: 什么是 webpack,它要...
    蕭玄辭閱讀 12,867評(píng)論 7 110
  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 webpack介紹和使用 一、webpack介紹 1、由來 ...
    it筱竹閱讀 11,443評(píng)論 0 21
  • 1. 初始化一個(gè)空項(xiàng)目 創(chuàng)建一個(gè)空文件夾 mkdir webpack-test 進(jìn)入這個(gè)文件夾 cd webpac...
    伐櫻大魔閱讀 954評(píng)論 0 0
  • 1 今天早晨起床較晚,但是家中的早餐還是很正確,喝了雪梨燉百合!感覺鼻子也正在好轉(zhuǎn),相信自己、積極地調(diào)整! 2 ...
    LiHongxi閱讀 182評(píng)論 0 0

友情鏈接更多精彩內(nèi)容