类型错误:无法读取未定义的'tapPromise'属性。

19

我正在使用 Angular Build Optimization 在 Angular 9 项目中,使用下面的代码生成打包文件,但是出现以下错误。我在Google上搜索了这个错误,但没有找到任何确切的答案。

正如错误提示所示,问题只存在于库本身。我已经进行了npm安装和npm审计修复,但仍然反复出现相同的错误。

错误详情

An error occurred during the build:
TypeError: Cannot read property 'tapPromise' of undefined
    at compiler.hooks.compilation.tap.compilation (E:\Code\blogui\BlogApp\node_modules\compression-webpack-plugin\dist\index.js:269:39)
    at SyncHook.eval [as call] (eval at create (E:\Code\blogui\BlogApp\node_modules\tapable\lib\HookCodeFactory.js:19:10), <anonymous>:25:1)
    at SyncHook.lazyCompileHook (E:\Code\blogui\BlogApp\node_modules\tapable\lib\Hook.js:154:20)
    at Compiler.newCompilation (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:631:26)
    at hooks.beforeCompile.callAsync.err (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:667:29)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (E:\Code\blogui\BlogApp\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (E:\Code\blogui\BlogApp\node_modules\tapable\lib\Hook.js:154:20)
    at Compiler.compile (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:662:28)
    at readRecords.err (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:321:11)

angular.json 文件

 "build": {
                    "builder": "@angular-builders/custom-webpack:browser",
                    "options": {
                        "outputPath": "dist/BlogApp",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "tsconfig.app.json",
                        "aot": true,
                        "customWebpackConfig": {
                            "path": "**src/custom-webpack.config.js**"
                        },
                        "assets": [
                            "src/favicon.ico",
                            "src/assets",
                            "src/sitemap.xml",
                            "src/robots.txt"
                        ],
                        "styles": [
                            "src/styles.scss"
                        ],
                        "scripts": ["./node_modules/jquery/dist/jquery.min.js"]
                    },
                    "configurations": {
                        "production": {
                            "fileReplacements": [{
                                "replace": "src/environments/environment.ts",
                                "with": "src/environments/environment.prod.ts"
                            }],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "buildOptimizer": true,
                            "budgets": [{
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                },
                                {
                                    "type": "anyComponentStyle",
                                    "maximumWarning": "6kb",
                                    "maximumError": "10kb"
                                }
                            ]
                        }
                    }
                },

自定义的webpack.config.js文件

const CompressionPlugin = require(`compression-webpack-plugin`);
const BrotliPlugin = require(`brotli-webpack-plugin`);
const path = require(`path`);
module.exports = {
    plugins: [
        new BrotliPlugin({
            asset: '[fileWithoutExt].[ext].br',
            test: /\.(js|css|html|svg|txt|eot|otf|ttf|gif)$/
        }),
        new CompressionPlugin({
            test: /\.(js|css|html|svg|txt|eot|otf|ttf|gif)$/,
            filename(info) {
                let opFile = info.path.split('.'),
                    opFileType = opFile.pop(),
                    opFileName = opFile.join('.');
                return `${opFileName}.${opFileType}.gzip`;
            }
        })
    ],
}


package.json文件

{
    "name": "blog-app",
    "version": "0.0.0",
    "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
    },
    "private": true,
    "dependencies": {
        "@angular-builders/custom-webpack": "^10.0.1",
        "@angular/animations": "~9.1.6",
        "@angular/common": "~9.1.6",
        "@angular/compiler": "~9.1.6",
        "@angular/core": "~9.1.6",
        "@angular/forms": "~9.1.6",
        "@angular/platform-browser": "~9.1.6",
        "@angular/platform-browser-dynamic": "~9.1.6",
        "@angular/router": "~9.1.6",
        "@syncfusion/ej2-angular-richtexteditor": "^18.2.56",
        "aws-sdk": "^2.797.0",
        "express-static-gzip": "^2.1.0",
        "jquery": "^3.5.1",
        "ng-lazyload-image": "^9.1.0",
        "ngx-spinner": "^10.0.1",
        "rxjs": "~6.5.4",
        "schema-utils": "^3.0.0",
        "tslib": "^1.10.0",
        "web-animations-js": "^2.3.2",
        "zone.js": "~0.10.2"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "^0.1100.3",
        "@angular/cli": "~9.1.5",
        "@angular/compiler-cli": "~9.1.6",
        "@types/jasmine": "~3.5.0",
        "@types/jasminewd2": "~2.0.3",
        "@types/node": "^12.19.6",
        "brotli-webpack-plugin": "^1.0.0",
        "codelyzer": "^5.1.2",
        "compression-webpack-plugin": "^7.0.0",
        "jasmine-core": "~3.5.0",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "~5.0.0",
        "karma-chrome-launcher": "~3.1.0",
        "karma-coverage-istanbul-reporter": "~2.1.0",
        "karma-jasmine": "~3.0.1",
        "karma-jasmine-html-reporter": "^1.4.2",
        "protractor": "^7.0.0",
        "ts-node": "~8.3.0",
        "tslint": "~6.1.0",
        "typescript": "~3.8.3"
    }
}

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["node"]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ],
  "angularCompilerOptions": {
    "enableIvy": false
  }
}

2个回答

44

我曾遇到过这个问题,但是是在使用React时。原因是压缩插件版本与Webpack版本不兼容。

在你的情况下,我认为你是通过"@angular-builders/custom-webpack"间接使用Webpack的,因为我在你的package.json中没有看到它。

你的Webpack是否来自以下源?

"@angular-builders/custom-webpack": "^10.0.1" -> "webpack-merge": "^4.2.2" -> "webpack": "^5.3.2"

无论如何,"compression-webpack-plugin": "^7.0.0"需要"webpack": "^5.9.0"。所以我建议你降级到"compression-webpack-plugin": "^6.0.5",它依赖于"webpack": "^5.3.2"。

尝试使用该版本或更低版本查看结果。

Credits

更新

这并不改变我的结论:降级。

一年多后,我再次检查Webpack的可能来源,显然我犯了一个错误,因为现在我发现"webpack-merge": "^4.2.2"依赖于"webpack": "^1.15.0",而不是"webpack": "^5.3.2"。请注意,这是一个开发依赖项(devDependencies)。

我还找到了另一个可能的Webpack来源,这对我来说更有意义:
"@angular-devkit/build-angular": "^0.1100.3" -> "webpack": "4.44.2"。请注意,"@angular-devkit/build-angular"是"@angular-builders/custom-webpack"的一个依赖项。

此外,你分享的错误消息的第六行给了我们一个很好的想法,并确认了之前的假设:

(...) @angular-builders\custom-webpack\node_modules\webpack\ (...)

请注意,"custom-webpack"没有直接依赖于"webpack"。

现在,"compression-webpack-plugin": "^7.0.0"实际上具有以下依赖项:

peerDependencies: { "webpack": "^5.1.0"}

devDependencies: {"webpack": "^5.9.0"}

另一方面,"compression-webpack-plugin": "^6.0.5"实际上具有以下依赖项:
peerDependencies: { "webpack":  "^4.0.0 || ^5.0.0"}

devDependencies: {"webpack": "^5.3.2"}

据我所知,compression-webpack-plugin是webpack的一个插件,因此我认为这种情况下重要的依赖关系是peer,它告诉我们支持的webpack版本。这就解释了为什么"compression-webpack-plugin": "^6.0.5"可以工作(peer dependency "webpack": "^4.0.0 || ^5.0.0",并通过build-angular: "^0.1100.3"你有"webpack": "4.44.2")。
我通过查看许多"package.json"、安装包、阅读博客以及使用NPMGraph(查看包的依赖关系图)来学习到所有这些内容。依赖关系比我想象的更加复杂,因此我不排除在某些方法上出错的可能性,欢迎指正。

你如何知道哪些版本与其他软件包不匹配或匹配。我总是需要搜索StackOverflow的问题来回答这个问题,但如果有一个网站或命令可以直接告诉我就好了。 - Ian Steffy
@IanSteffy 如何查看给定npm模块的依赖树?。请注意第一条评论。 - eniel.rod
我在使用React时遇到了问题,由于我正在使用CRA,因此更难进行故障排除,因为我正在使用Craco作为Webpack覆盖程序..必须找到解决方案。 - Code Drop
降级CompressionPlugin版本对我有用。我从10降级到6。Webpack 4。 - mkupiniak

3

在使用 REACT 进行开发时,我遇到了这个问题。我用的是:

npm audit fix --force

更改了两个内容:

compression-webpack-plugin 从 ^2.0.0 版本升级到 ^8.0.1 版本

webpack-dev-server 从 ^3.11.0 版本升级到 ^0.64.2 版本

这个命令导致我的开发系统崩溃,出现以下错误序列:

  1. Error: Cannot find module 'webpack/bin/config-optimist' 然后,我将 webpack-dev-server 降级回 ^3.11.0 版本,错误被修改。
  2. TypeError: Cannot read property 'tapPromise' of undefined 然后,我将 compression-webpack-plugin 降级回 ^2.0.0 版本。

问题得到解决。


它对我不起作用。 - Tatyana Molchanova
它让我的应用程序启动了,但在捆绑时没有停止加载。 - Code Drop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接