账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    webpack打包报错Cannot read property 'emit' of undefined
    37
    0

    webpack使用compression-webpack-plugin插件的时候报错

    相关代码

    const CompressionPlugin = require('compression-webpack-plugin')

    webpackConfig.plugins.push(

    new CompressionPlugin({
      // asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(js)$' 
        // '\\.(' +
        // configItem.productionGzipExtensions.join('|') +
        // ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
    
    

    错误如下

    compiler.hooks.emit.tapAsync({ name: 'CompressionPlugin' }, emit);

                   ^
    

    TypeError: Cannot read property 'emit' of undefined

    at CompressionPlugin.apply (C:\MyWayProject\2018\vue\ehrAdmin\node_modules\compression-webpack-plugin\dist\index.js:175:20)
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 喵了个咪。 普通会员 1楼

      这个错误通常发生在你在使用webpack编译时,尝试访问一个未定义的属性。这个属性可能是在当前组件或者父组件中定义的,但可能因为某些原因未被正确地赋值。

      例如,如果你在webpack.config.js文件中设置了output.filename,但你尝试访问output.filename,那么就会出现这个错误。

      解决这个问题的方法是在访问output.filename之前,检查它是否已经被定义。例如,你可以这样做:

      javascript if (output.filename) { // 输出文件的路径 } else { // 如果输出文件没有定义,那么就默认输出文件为你的入口文件 console.log('Output file is not defined'); }

      此外,你也可以在webpack.config.js文件中添加一个入口文件,这样即使输出文件没有定义,webpack也可以正常工作。

      javascript module.exports = { // ... entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, // ... };

      然后在你的index.js文件中引入这个入口文件:

      ```javascript import path from 'path'; import bundle from './bundle.js';

      // 这里可以执行你的代码,然后在bundle.js中导入你的模块 ```

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部