Ubuntu 20 EC2实例Ionic环境升级后生成Android/IOS平台文件夹报错求助
Alright, let's dig into your issue—you're hitting two frustrating errors after upgrading your Ionic/Angular/Cordova/Node stack on an EC2 instance, right? Let's break down why this is happening and walk through actionable fixes:
Previously, you could successfully generate Android/iOS platform folders for build transfer using:
- Ionic CLI 6.18.1, @ionic/angular 5.9.3
- Cordova CLI 10.0.0, Node.js 14.18.1
After upgrading to:
- Ionic CLI 6.18.1, @ionic/angular 6.0.8
- Cordova CLI 11.0.0, Node.js 16.14.0
You're now facing two persistent errors on your EC2 instance:
- FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
- Error at new SubprocessError
1. JavaScript Heap Out of Memory
- Node.js 16 uses an updated V8 engine with different memory management logic compared to Node.js 14. Angular 6's production build (with aggressive tree-shaking, code compression, and AOT compilation) is more memory-intensive than Angular 5, which pushes your EC2 instance's memory limits over the edge.
- The Cordova 11 upgrade likely introduced new dependencies or modified build workflows that add to the memory footprint during platform preparation.
2. SubprocessError
This is almost certainly a secondary issue triggered by the heap memory error. When the Angular/Ionic build process crashes from OOM, it causes the child subprocess (handled by Ionic's utils-subprocess) to exit unexpectedly, throwing this error. It could also stem from minor compatibility gaps between Cordova 11 and Node.js 16, or permission/resource constraints on your EC2 instance.
You tried addressing the memory issue by setting NODE_OPTIONS=--max_old_space_size=8192 and adding a high-memory build script to package.json:
"scripts": { "ng-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --configuration=prod", }
And ran npm run ng-high-memory—but the problem persists.
For the Heap Memory Error
Crank up the memory allocation
Try increasing themax_old_space_sizeto 12GB (12288) to give the build process more headroom. Update your scripts to tie the build directly to Cordova platform preparation:"scripts": { "ionic-build-prod": "node --max_old_space_size=12288 ./node_modules/@ionic/cli/bin/ionic build --prod", "cordova-prepare-android": "npm run ionic-build-prod && ionic cordova prepare android", "cordova-prepare-ios": "npm run ionic-build-prod && ionic cordova prepare ios" }Run the relevant command for your platform, e.g.,
npm run cordova-prepare-android.Optimize Angular build settings
Tweak yourangular.jsonto reduce memory load during production builds:- Temporarily disable memory-heavy optimizers to test if they're the culprit (not recommended for production, but useful for debugging):
"configurations": { "production": { "buildOptimizer": false, "aot": false, // Keep your other production configs } } - Enable build caching to reuse previous build artifacts and reduce redundant work:
"cli": { "cache": { "enabled": true } }
- Temporarily disable memory-heavy optimizers to test if they're the culprit (not recommended for production, but useful for debugging):
Upgrade your EC2 instance
If you're using a low-memory instance (like t2.micro), upgrade to a larger instance type (e.g., t2.large or higher). This is a straightforward hardware-level fix for persistent memory constraints.
For the SubprocessError
Reset your dependencies and platforms
Corrupted dependencies or platform files often cause subprocess issues. Run these commands to clean up:rm -rf node_modules package-lock.json npm install ionic cordova platform remove android ios ionic cordova platform add android@latest ios@latestSwitch to a more stable Node.js 16 LTS version
Node.js 16.14.0 might have minor compatibility quirks with Cordova 11. Use nvm to switch to the latest 16.x LTS release:nvm install 16.20.2 nvm use 16.20.2Verify EC2 permissions
Ensure theec2-userhas full read/write access to critical build folders:sudo chown -R ec2-user:ec2-user ./platforms ./www
- Ionic Angular 6 Upgrade Guide: Focus on build workflow changes and dependency updates
- Cordova 10 → 11 Migration Docs: Review platform compatibility and build step adjustments
- Node.js 14 → 16 Migration Guide: Learn about V8 engine memory management changes
- Angular CLI Build Optimization Docs: Explore how to tweak build parameters to reduce memory usage
内容的提问来源于stack exchange,提问作者Suvel Rathneswar

