Static Site Generator Build Fails: Out of Memory Fixes
Your static site generator crashes with 'out of memory' during build. Here's the real fix — increase Node.js memory or slim down your content.
1. Node.js Runs Out of Memory During Build
This is the most common reason your static site generator (SSG) fails. You're using something like Gatsby, Next.js, or Hugo, and the build crashes with:
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
This happens because Node.js has a default memory limit of about 512 MB or 1 GB depending on your system. A big site with many pages, images, or plugins hits that limit fast.
How to fix it
Tell Node.js to use more memory. Open your terminal and set the NODE_OPTIONS environment variable before running the build command:
# On Linux or macOS
export NODE_OPTIONS="--max-old-space-size=4096"
# On Windows (Command Prompt)
set NODE_OPTIONS=--max-old-space-size=4096
# On Windows (PowerShell)
$env:NODE_OPTIONS="--max-old-space-size=4096"
Then run your build command again (like gatsby build or next build).
What to expect: After setting this, the build should complete without the memory error. If it still crashes, increase the number — try 6144 (6 GB) or 8192 (8 GB).
If you're using a CI/CD platform like Netlify or Vercel, you can set this in your environment variables section. On Netlify, go to Site settings > Build & deploy > Environment, and add a variable named NODE_OPTIONS with value --max-old-space-size=4096.
2. Images and Assets Are Too Large
Another common cause: your SSG processes images during build. A single 10 MB image won't break it, but 50 images at 5 MB each will. The build tries to generate thumbnails, resize, and optimize every image in memory. That adds up fast.
How to fix it
Resize and compress your images before the build. Here's what I do:
- Use a tool like ImageOptim (Mac) or RIOT (Windows) to reduce file sizes.
- Set a max width on your images. For blog posts, 1200px wide is plenty. For gallery images, 2000px max.
- Convert to WebP format where possible — it's smaller than JPEG or PNG.
- If your SSG has an image optimization plugin (like
gatsby-plugin-image), configure it to limit the max resolution.
One more thing: Check your src folder for any huge files you don't need. I once found a 200 MB PDF that was accidentally included. Deleting it fixed the build instantly.
After cleaning up your images, run the build again. It should complete much faster and use less memory.
3. Too Many Plugins or Recursive Content
Third cause: your SSG is doing too much work because of plugins or content loops. For example, in Gatsby, having 20+ plugins that each process every page can triple memory usage. Or you have a plugin that queries all pages and generates a list — then another plugin does the same thing again.
Another sneaky problem: recursive content. If your content structure has pages that include other pages, and those pages include back, you get an infinite loop. The build never finishes and memory fills up.
How to fix it
Audit your plugins. Disable them one by one and rebuild to see which one is the problem. I've seen the gatsby-remark-images plugin cause memory issues when processing hundreds of images in Markdown files. Switching to a lighter version or limiting it to only process images below a certain size helped.
To check for recursive content:
- Look at your templates or layouts. Do they include partials that include the same template? Example: a
header.htmlthat includesnav.html, andnav.htmlincludesheader.html. That's recursion. - Check your content files (Markdown, JSON, YAML) for any references that loop back to themselves. Like a page that has a field pointing to itself.
- If you're using a CMS headless setup, make sure your content API doesn't return circular references.
Quick test: Create a minimal version of your site — just a few pages and no plugins. If it builds fine, add plugins back one by one. The moment the build crashes, you found the culprit.
Quick-Reference Summary Table
| Cause | Symptom | Fix |
|---|---|---|
| Node.js memory limit too low | "FATAL ERROR: Reached heap limit" | Set NODE_OPTIONS=--max-old-space-size=4096 |
| Large images or assets | Build slows down, then crashes | Compress images, limit max width |
| Too many plugins or recursion | Build hangs, memory usage climbs | Disable plugins one by one, fix loops |
Was this solution helpful?