Fix 'JavaScript Heap Out of Memory' for Gatsby on Netlify
Also appears as:
- ENOMEM: not enough memory
- FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed
- JavaScript heap out of memory gatsby build
- Killed - gatsby build out of memory
Symptoms
- Build crashes during 'gatsby build' step
- Build log shows memory allocation errors
- Build works locally but fails on Netlify
- Build fails at the image processing or page generation stage
What Causes This Error
Too many images being processed simultaneously
CommonGatsby's sharp-based image processing loads all images into memory. Sites with hundreds of images can easily exceed Netlify's memory limit during the build.
Large number of pages generated at build time
CommonGatsby keeps all page data in memory during the build. Sites with thousands of pages (especially with heavy data) can exhaust the available heap space.
Heavy GraphQL queries
OccasionalLarge GraphQL queries that fetch all data at once hold everything in memory. This compounds with the page count as each page might execute heavy queries.
Memory leaks in plugins
OccasionalSome Gatsby plugins have known memory leaks that accumulate during the build process. Older versions of gatsby-plugin-image and source plugins are common culprits.
How to Fix It
Fix 1: Increase Node.js memory limit
- Go to your Netlify site's Environment Variables settings
- Add or update the NODE_OPTIONS variable
- Set the value to increase the heap size
- Trigger a new deploy
NODE_OPTIONS=--max_old_space_size=4096Verify: Redeploy and check if the build completes. If it still fails, try 8192 instead of 4096.
Fix 2: Optimize image processing
- Reduce the number of image sizes/formats generated
- Use gatsby-plugin-image with constrained layout instead of fullWidth where possible
- Compress source images before adding them to your project
- Process images in smaller batches using GATSBY_CPU_COUNT=1
NODE_OPTIONS=--max_old_space_size=4096
GATSBY_CPU_COUNT=2Verify: Rebuild and check if memory usage stays within limits during the image processing step.
Fix 3: Enable incremental builds
- Install the gatsby-plugin-netlify build cache plugin
- Enable Gatsby's experimental incremental builds
- This reduces memory usage by only rebuilding changed pages
GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=trueVerify: After the first full build, subsequent builds should be faster and use less memory.
Fix 4: Optimize GraphQL queries
- Use GraphQL query limits and pagination
- Avoid fetching all nodes at once in gatsby-node.js
- Use createPages pagination to batch page creation
- Remove unused GraphQL fields from your queries
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMarkdownRemark(limit: 100, skip: 0) {
nodes {
fields { slug }
}
}
}
`)
// Process in batches
}Verify: Check that queries fetch only necessary data and pages are created in batches.
How to Prevent This
Monitor your build memory usage in Netlify's build logs — look for peak memory usage.
Optimize images before adding them to your repository (compress, resize to maximum needed dimensions).
Keep Gatsby and its plugins updated — newer versions often have better memory management.
Use the Netli.fyi app to monitor build health and catch memory issues early.
Consider migrating from Gatsby to Astro or Next.js for very large sites — they handle memory more efficiently at scale.

Developer Advocate at RevenueCat and creator of Netli.fyi. Building on Netlify since 2019. Writes from hands-on experience deploying dozens of production sites.
Manage Netlify on the go
Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.