All posts

Fix MemoryError in Angular When Deploying

Resolve out-of-memory errors during Angular build and deployment caused by large bundles, AOT compilation, and CI memory limits.

Angular Build Memory Errors During Deployment

Angular's AOT compiler and webpack bundling can consume enormous amounts of memory during the build step, causing CI/CD pipelines to fail with FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.

Increase Node.js Heap

The default heap limit (~1.7GB) isn't enough for large Angular projects:

# In your CI pipeline
export NODE_OPTIONS="--max-old-space-size=4096"
ng build --configuration production

Or in package.json:

{
  "scripts": {
    "build:prod": "node --max-old-space-size=4096 node_modules/@angular/cli/bin/ng build --configuration production"
  }
}

Reduce Bundle Size

Large bundles require more memory to process:

# Analyze your bundle
ng build --stats-json
npx webpack-bundle-analyzer dist/stats.json

Common savings:

  • Lazy load feature modules
  • Remove unused imports (tree-shaking helps, but barrel files can defeat it)
  • Replace heavy libraries (moment.js → date-fns)

CI Configuration

# GitHub Actions
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm run build:prod
        env:
          NODE_OPTIONS: --max-old-space-size=4096

Docker Build Memory

ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN npm run build:prod

Ensure your Docker builder has enough memory allocated (Docker Desktop defaults to 2GB).

Bugsly's CI integration tracks build failures and deployment errors, so memory-related build failures are surfaced alongside runtime errors in a single dashboard.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free