构建Angular项目时遇TypeError及npm依赖冲突问题求助
Angular项目构建错误解决方案
错误1:TypeError: (0 , os_1.availableParallelism) is not a function
错误日志
[error] TypeError: (0 , os_1.availableParallelism) is not a function at Object.<anonymous> (C:\Users\anike\OneDrive\ecommerce-project\03-frontend\angular-ecommerce\node_modules\piscina\dist\src\index.js:37:54) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object.<anonymous> (C:\Users\anike\OneDrive\ecommerce-project\03-frontend\angular-ecommerce\node_modules\piscina\dist\src\main.js:5:33) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
原因与解决
该错误源于piscina包调用了Node.js 19.0.0及以上版本才新增的os.availableParallelism() API,而当前使用的Node.js版本低于19。不过这是依赖版本不匹配引发的连锁问题,优先解决第二个依赖冲突后,此错误大概率会自动消失。若单独处理,可二选一:
- 升级Node.js到19.0.0或更高版本;
- 在
package.json中添加版本覆盖规则,指定兼容旧Node的piscina版本:
执行"overrides": { "piscina": "^3.2.0" }npm install生效。
错误2:npm依赖树解析错误(peer依赖版本不匹配)
错误日志
Command failed: npm i --package-lock-only --prefix C:\Users\anike\AppData\Local\Temp\exhort_gaocBM npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: angular-ecommerce@0.0.0 npm ERR! Found: @angular/compiler-cli@14.3.0 npm ERR! node_modules/@angular/compiler-cli npm ERR! dev @angular/compiler-cli@"^14.0.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer @angular/compiler-cli@"^18.0.0" from @angular-devkit/build-angular@18.0.0 npm ERR! node_modules/@angular-devkit/build-angular npm ERR! dev @angular-devkit/build-angular@"^18.0.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See C:\Users\anike\AppData\Local\npm-cache\eresolve-report.txt for a full report. npm ERR! A complete log of this run ...
原因与解决
核心问题是@angular-devkit/build-angular版本必须与Angular核心包(如@angular/compiler-cli)版本完全匹配。当前项目使用Angular 14,却安装了Angular 18版本的构建工具,导致peer依赖冲突。提供两种解决方案:
方案1:降级构建工具到Angular 14兼容版本
- 修改
package.json的devDependencies部分,将@angular-devkit/build-angular版本改为^14.2.13(与@angular/cli版本一致):"devDependencies": { "@angular-devkit/build-angular": "^14.2.13", // 其他依赖保持不变 } - 清理旧依赖并重新安装:
# Windows系统 rmdir /s /q node_modules del package-lock.json npm install # macOS/Linux系统 rm -rf node_modules package-lock.json npm install
方案2:升级整个项目到Angular 18
若想跟进最新版本,执行官方升级命令:
ng update @angular/cli@18 @angular/core@18
注意:升级前建议备份项目,Angular 14的部分API在新版本中已废弃,需对应调整代码。
内容的提问来源于stack exchange,提问作者Aniket Sharma
相关产品推荐
相关产品推荐

