Firebase App Hosting部署Angular项目时ng:not found错误求助
解决Angular单仓库工作区部署Firebase App Hosting时的
ng: not found及npx执行错误 核心原因
Firebase Cloud Build环境中,单仓库工作区的依赖安装与命令执行路径不匹配:要么全局未安装Angular CLI,要么npx无法定位到工作区根目录的本地CLI依赖,同时未明确指定目标项目导致命令混淆。
解决方案
方案1:直接调用工作区根目录的本地Angular CLI
修改organizer项目的package.json中的build脚本,明确指向工作区根目录的本地CLI,并指定目标项目:
"build": "node ../node_modules/@angular/cli/bin/ng build organizer --configuration production"
- 优势:无需依赖全局CLI,直接使用工作区已安装的依赖,避免路径识别问题。
方案2:统一在工作区根目录执行构建流程
- 在工作区根目录的
package.json中添加针对organizer的构建脚本:
"scripts": { "build:organizer": "ng build organizer --configuration production" }
- 修改
firebase.json的hosting配置,确保在根目录执行前置构建命令:
{ "hosting": { "source": ".", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "frameworksBackend": { "region": "us-central1" }, "predeploy": [ "npm run build:organizer" ] } }
- 优势:Cloud Build会在根目录执行
npm install,自动安装所有工作区依赖(包括Angular CLI),从根源解决命令找不到的问题。
方案3:强制npx使用工作区根目录的CLI依赖
如果坚持使用npx,修改organizer项目的build脚本,指定依赖路径:
"build": "npx -p ../node_modules/@angular/cli ng build organizer --configuration production"
额外检查项
- 确认工作区根目录的
package.json中已将@angular/cli列为开发依赖(而非仅在organizer项目的package.json中)。 - 查看Cloud Build日志,确保
npm install步骤是在工作区根目录执行,而非进入organizer子目录单独执行。
内容的提问来源于stack exchange,提问作者Simple Dev
相关产品推荐
相关产品推荐

