我在Angular 9项目中使用Angular Build Optimization,并使用以下代码生成捆绑包,但出现以下错误。我用谷歌搜索了一下这个错误,但没有找到任何具体的答案。
正如错误所表明的那样,问题只出在图书馆本身。我也做了npm安装和npm审计修复,但一次又一次地得到同样的错误。
错误详细信息
An error occurred during the build:
TypeError: Cannot read property 'tapPromise' of undefined
at compiler.hooks.compilation.tap.compilation (E:CodebloguiBlogAppnode_modulescompression-webpack-plugindistindex.js:269:39)
at SyncHook.eval [as call] (eval at create (E:CodebloguiBlogAppnode_modulestapablelibHookCodeFactory.js:19:10), <anonymous>:25:1)
at SyncHook.lazyCompileHook (E:CodebloguiBlogAppnode_modulestapablelibHook.js:154:20)
at Compiler.newCompilation (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:631:26)
at hooks.beforeCompile.callAsync.err (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:667:29)
at AsyncSeriesHook.eval [as callAsync] (eval at create (E:CodebloguiBlogAppnode_modulestapablelibHookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (E:CodebloguiBlogAppnode_modulestapablelibHook.js:154:20)
at Compiler.compile (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:662:28)
at readRecords.err (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.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"
}
]
}
}
},
customizedwebpack.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
}
}
我遇到了这个问题,但使用了React。原因是压缩插件版本和Webpack版本之间不兼容。
在你的情况下,我认为你使用Webpack";间接地";通过"@angular-builders/custom-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"
。
试试那个版本或更低的版本,看看会发生什么。
信用
更新
这并不能改变我的结论:降级
一年多后,我发现自己再次检查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\自定义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"
工作的原因(对等依赖"webpack": "^4.0.0 || ^5.0.0"
,通过build-angular: "^0.1100.3"
可以获得"webpack": "4.44.2"
(。
所有这些我都是通过复习许多";package.json";,安装软件包、阅读博客和使用NPMGraph(请参阅软件包的依赖关系图(
依赖关系比我想象的更复杂,所以我不排除某些方法是错误的,欢迎更正。
我在使用REACT进行开发时遇到了这个问题。我用过:
npm audit fix --force
它改变了两件事:
压缩webpack插件从^2.0.0到^8.0.1
webpack开发服务器从^3.11.0到^0.64.2
该命令破坏了我的开发系统,并按以下顺序抛出错误:
Error: Cannot find module 'webpack/bin/config-optimist'
然后,我将webpack-dev服务器恢复到^3.11.0,并修改了错误TypeError: Cannot read property 'tapPromise' of undefined
然后我将压缩webpack插件恢复到^2.0.0
它解决了问题。