如何解决Angular12安装@nguniversal/express-engine报错问题
问题场景
在Angular 12项目中执行@nguniversal/express-engine安装命令时报错,无法完成安装。
环境版本信息
Npm : = 7.5.4 Angular CLI: 12.2.5 Node: 14.17.6
报错信息
> D:\Project\rkp>ng add @nguniversal/express-engine --clientProject projectname > npm version 7.5.4 detected. When using npm 7 with the Angular CLI, npm > version 7.5.6 or higher is recommended. i Using package manager: npm √ > Found compatible package version: @nguniversal/express-engine@12.1.0. > √ Package information loaded. > > The package @nguniversal/express-engine@12.1.0 will be installed and > executed. Would you like to proceed? Yes npm ERR! code ERESOLVE npm > ERR! Cannot destructure property 'name' of 'node' as it is null. > > npm ERR! A complete log of this run can be found in: npm ERR! > C:\Users\admin\AppData\Local\npm-cache\_logs\2021-09-25T13_19_38_918Z-debug.log > × Package install failed, see above.
报错原因
该报错为npm 7.5.4版本的已知依赖解析缺陷,叠加Angular CLI对npm 7系列的最低版本要求不匹配导致,具体触发点为npm解析依赖树时遇到空节点,无法完成属性解构操作。
解决方案
- 方案1:升级npm到兼容版本
执行全局升级命令:npm install -g npm@7.5.6
升级完成后清理npm缓存:npm cache clean --force
重新执行原安装命令即可正常完成安装。 - 方案2:添加参数绕过依赖解析校验
无需升级npm的场景下,在原安装命令后追加--legacy-peer-deps参数,绕过npm 7的严格对等依赖校验规则:ng add @nguniversal/express-engine --clientProject projectname --legacy-peer-deps
若仍触发报错,可额外追加--force参数强制执行安装。 - 方案3:分步执行安装与初始化
先手动安装匹配版本的依赖包:npm install @nguniversal/express-engine@12.1.0 --legacy-peer-deps
依赖安装完成后,单独执行初始化逻辑:ng generate @nguniversal/express-engine:setup --clientProject projectname
若以上操作均未解决问题,可先删除项目根目录下的node_modules文件夹和package-lock.json文件,执行npm install重新安装现有项目依赖后,再尝试上述安装操作。
内容的提问来源于stack exchange,提问作者Smart Code
相关产品推荐
相关产品推荐

