旧Ionic Angular项目安卓运行报错问题求助
问题描述
- 旧Ionic项目原使用Node.js v14.16.1,因Node.js 14于2023-04-30终止支持,升级至Node.js v18.19.0
- 执行命令
ionic cordova run android时触发错误:在Angular CLI工作区外无法执行该命令,重装Angular CLI后问题未解决
项目环境信息
Ionic
- Ionic CLI: 5.4.16
- Ionic Framework: ionic-angular 3.9.5
- @ionic/app-scripts: 3.2.3
Capacitor
- Capacitor CLI: 2.4.7
- @capacitor/core: 2.4.7
Cordova
- Cordova CLI: 11.0.0
- Cordova Platforms: android 9.1.0
- Cordova Plugins: cordova-plugin-ionic-keyboard 2.1.3、cordova-plugin-ionic-webview 4.0.1(及其他26个插件)
工具
- cordova-res: 0.15.3(可升级至0.15.4)
- native-run: 1.5.0(可升级至1.7.1)
系统
- Android SDK Tools: 26.1.1
- NodeJS: v14.16.1(已升级至v18.19.0)
- npm: 6.14.12
- OS: Windows 10
报错信息
[INFO] Hardware device(s) found for android. Using --device.
ng.cmd run app:ionic-cordova-build --platform=android
Error: This command is not available when running the Angular CLI outside a workspace.
[ERROR] An error occurred while running subprocess ng.ng.cmd run app:ionic-cordova-build --platform=android exited with exit code 1. Re-running this command with the --verbose flag may provide more information.
解决方案
1. 匹配Angular CLI版本与项目兼容性
Ionic 3对应低版本Angular(5/6左右),Node.js 18搭配的高版本Angular CLI会出现兼容问题,需安装适配版本:
- 全局卸载当前Angular CLI:
npm uninstall -g @angular/cli - 安装兼容的Angular CLI v6.x版本:
npm install -g @angular/cli@6.2.9 - 项目本地也安装对应版本:
npm install @angular/cli@6.2.9 --save-dev
2. 修正Ionic配置文件
Ionic 3依赖@ionic/app-scripts构建,而非Angular CLI工作区模式,需确保配置正确:
- 打开项目根目录的
ionic.config.json,确认配置包含:
{ "name": "你的项目名称", "type": "ionic-angular", "integrations": { "cordova": {} } }
- 若存在
cli相关配置,移除可能冲突的项,避免Ionic CLI错误调用Angular CLI命令。
3. 清理依赖并重装
- 删除项目根目录的
node_modules文件夹和package-lock.json文件 - 清理npm缓存:
npm cache clean --force - 重新安装依赖:
npm install
4. 直接使用ionic-app-scripts构建运行
绕过Ionic CLI的ng命令调用逻辑,直接用原生构建工具:
- 构建项目:
ionic-app-scripts build --prod --platform android - 运行Cordova命令:
cordova run android --device
5. 切换至更兼容的Node.js版本
Node.js 18对Ionic 3和Cordova 11支持有限,可尝试Node.js 16(LTS版本):
- 用nvm安装Node.js 16.20.2:
nvm install 16.20.2 - 切换版本:
nvm use 16.20.2 - 重新安装依赖并运行项目
内容的提问来源于stack exchange,提问作者abdul
相关产品推荐
相关产品推荐

