November 25, 2026 · 9 min read · Thomas Osayi
JavaScript heap out of memory during next build: causes and fixes
Why next build fails with JavaScript heap out of memory, how to raise the limit with NODE_OPTIONS, how much memory Vercel builds get, and how to find the real cause.
Few build errors look as scary as a wall of garbage collector output ending in FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory. It usually appears after a project has grown for a while, often right after an AI tool added a big library or a page that generates thousands of routes.
The good news is that this one is rarely mysterious. The build ran out of room, and there are only a handful of things in a Next.js build that use a lot of it. This post is only about the heap error. For the wider list of deploy failures, "Vercel build failed? The most common Next.js deploy errors and fixes" covers the rest.
What does "JavaScript heap out of memory" mean in a Next.js build?
It means the Node.js process running next build tried to use more memory than its heap limit allows, so Node stopped the process.
Next.js runs on Node.js, and Node gives JavaScript a heap with a size limit. When compiling, bundling and prerendering pushes memory past that limit and garbage collection cannot free enough, Node prints a fatal error and exits. You will usually see one of these lines near the end of the log:
- FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
- FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
On a hosting platform you may see a different ending: the build simply stops with SIGKILL or an out of memory message. That version means the whole machine ran out of memory and the operating system killed the process, which is a related but slightly different problem. The heap error says Node's limit was too low for the work. The SIGKILL says the machine itself was too small, often because the heap limit was set higher than the RAM available.
Is the problem my code or the build machine?
It is almost always both: the build got bigger than the machine, and something in the code made it bigger than it needed to be.
A quick way to tell which side to start on:
- If the build passes on your laptop and fails on the host, the host has less memory or a colder cache. Start with the limits below.
- If it fails everywhere, something in the project is using far more memory than normal. Start with the causes further down.
- If it started failing right after one commit, look at that commit first. A new dependency, a new generateStaticParams function or a change to next.config.js is the usual suspect.
That last case is the most common one I see in AI-built apps. An agent adds a charting library, an icon pack or a page that prebuilds every product, and the build that took two minutes yesterday now dies at the "Collecting page data" or "Generating static pages" step. Reading the last few lines before the crash tells you which phase was running, which narrows the cause a lot.
How do I raise the memory limit with NODE_OPTIONS?
Set the NODE_OPTIONS environment variable to --max-old-space-size followed by a size in megabytes, for example --max-old-space-size=4096.
Locally, you can put it in your build script in package.json so the build command becomes NODE_OPTIONS=--max-old-space-size=4096 next build. That syntax works on macOS and Linux. On Windows, use a helper package such as cross-env or set the variable in your terminal first.
On Vercel, add an environment variable in Project Settings under Environment Variables with the name NODE_OPTIONS and the value --max-old-space-size=6144, then redeploy. Vercel's own troubleshooting guide suggests 6144 for its standard build containers, which have 8,192 MB of memory.
The rule is to stay below the machine's real memory. Node needs memory outside the heap, and your build may start worker processes too. If you set the heap to 8192 on an 8 GB machine, you trade the heap error for a SIGKILL.
How much memory does a Vercel build get?
At the time of writing, Hobby builds run on a Basic machine with 2 vCPUs and 8 GB of memory, and Pro and Enterprise teams can use machines with up to 60 GB.
Vercel's build docs list these machine types:
- Basic: 2 vCPUs, 8 GB memory, used for every Hobby build.
- Standard: 4 vCPUs, 8 GB memory.
- Enhanced: 8 vCPUs, 16 GB memory.
- Turbo: 30 vCPUs, 60 GB memory.
- Elastic: 4 to 30 vCPUs and 8 to 60 GB, sized automatically per project.
New Pro and Enterprise accounts use Elastic by default. According to the docs, Elastic considers how much memory a build uses, upgrades a build that is close to running out of memory, and runs the next deployment after an out of memory failure on a higher tier. Builds are billed by CPU minute on paid teams, starting at $0.0035 per CPU minute, so a bigger machine costs more per minute but can finish faster.
If you are on Hobby, the ceiling is fixed at 8 GB, which is why trimming the build matters more there. Build Diagnostics in the project's Observability tab shows how build resource use changed between deployments, which helps you spot when the jump happened.
What uses the most memory in a Next.js build?
Prerendering many pages, importing large libraries, generating source maps and type checking are the usual heavy hitters.
Static generation at scale
A generateStaticParams function that returns every product, post or user page makes Next.js render all of them during the build. Thousands of pages, each fetching data, add up fast. Return only your most visited pages and let the rest render on demand, which keeps the build small while users still get every page.
Giant imports
Importing a whole icon library, a full lodash, a date library with every locale or a big SDK into a shared layout pulls it into many bundles. Import the specific function or icon you need, and keep heavy libraries out of the root layout.
Source maps
Source maps can take a lot of memory to produce. The Next.js memory guide lists productionBrowserSourceMaps, experimental.serverSourceMaps and enablePrerenderSourceMaps as options you can set to false.
Type checking and custom webpack config
The "Running TypeScript" step uses memory on large codebases. A custom webpack function in next.config.js that adds plugins, or something that accidentally keeps data around between compilations, can also grow memory steadily until the build dies.
How do I find exactly what is eating memory?
Run the build with Next.js's memory debugging flag first, and take a heap profile if that is not enough.
The Next.js memory usage guide documents these tools:
- next build --experimental-debug-memory-usage prints memory and garbage collection stats while the build runs, and takes heap snapshots as it nears the limit. The guide notes it does not work together with the webpack build worker.
- node --heap-prof node_modules/next/dist/bin/next build writes a .heapprofile file you can load in the Memory tab of Chrome DevTools to see which functions allocated the most.
- Starting the build with NODE_OPTIONS=--inspect lets you attach Chrome DevTools and take heap snapshots by hand.
In practice, the debug flag alone usually answers the question. Watch which phase memory climbs in. A steady climb during compilation points at imports and webpack config. A climb during "Generating static pages" points at prerendering. Then remove the suspect, such as commenting out a generateStaticParams or a heavy import on a branch, and build again to confirm.
Which next.config settings reduce build memory?
The Next.js guide lists a few switches, and each one trades something for lower memory.
- experimental.webpackMemoryOptimizations set to true, available from Next.js 15.0.0, lowers peak webpack memory at a small cost in compile time. The docs describe it as low risk.
- experimental.webpackBuildWorker runs webpack in a separate worker. It has been on by default since 14.1.0 when you do not have a custom webpack config, so removing an unneeded custom config can turn it back on.
- Turning off the source map options above cuts memory if you do not need production source maps.
- typescript.ignoreBuildErrors set to true skips type checking in the build. Only do this if a separate CI step runs the type check, because otherwise type errors ship.
Note that the webpack switches only matter if your build actually runs webpack. Check the top of your build log for which bundler it used before changing them.
What does a real fix look like?
Here is an illustration rather than a client story: an online shop built with an AI tool has 4,000 products, and its generateStaticParams returns all of them.
The build passed for months with a few hundred products, then started failing on Vercel Hobby with the heap error during "Generating static pages". Raising NODE_OPTIONS to 6144 got one deploy through, and the next week it failed again as products were added.
The lasting fix had three parts. First, generateStaticParams was changed to return the 200 best sellers, with the other product pages rendered on the first visit and cached. Second, the product page was importing an entire icon set and a full charting library for a small size chart, and both were swapped for targeted imports. Third, production browser source maps were turned off because nobody was using them. Build memory dropped well below the limit, and the NODE_OPTIONS override could stay as a safety margin instead of a crutch.
That same pattern, fewer prerendered pages, smaller imports and only the source maps you need, fixes most of these builds. On a stack like the TheHoopGang creator portal, which is Next.js with Prisma and Postgres, the same habits apply: keep data heavy pages dynamic unless they really benefit from being built ahead of time.
When is it worth paying for a bigger machine or getting help?
Pay for a bigger machine when the project is legitimately large and you have already trimmed it, and get help when you cannot tell what is using the memory.
A big app with many routes and a real need for prerendering may simply need more than 8 GB, and a Pro team on Elastic or Enhanced machines is a reasonable answer. But if a small app is hitting the limit, a bigger machine only hides a problem that will come back.
If your deploy is blocked today, my Quick Fix starts at $60 and takes about 2 days for one bug like this. If the memory error is one of several problems before launch, Fix Plus Features starts at $200 over about 5 days. Related posts worth a look are "Vercel function timeout (FUNCTION_INVOCATION_TIMEOUT): causes and fixes" and "Environment variables undefined on Vercel? Every cause and fix", since those often show up in the same projects.
The bottom line
"JavaScript heap out of memory" during next build means Node ran out of heap while compiling or prerendering. Setting NODE_OPTIONS to --max-old-space-size, kept below the machine's memory, gets you unstuck, and Vercel's 8 GB Hobby ceiling is the limit to design around. The real fix is finding the cause with next build --experimental-debug-memory-usage, then prerendering fewer pages, importing less and dropping source maps you do not use.
Questions, answered in one line
What value should I use for max-old-space-size?
Use a number in megabytes that leaves room for the rest of the system, such as 4096 on a 6 to 8 GB machine; Vercel suggests 6144 for its standard 8 GB build containers.
Why does next build work on my laptop but run out of memory on Vercel?
Your laptop may have 16 GB or more and a warm cache, while a Hobby build on Vercel has 8 GB and often starts cold, so a build near the limit locally will fail there.
Is it safe to turn off TypeScript checking in the build to save memory?
It saves memory, but type errors can then reach production, so only do it if you run type checks separately in CI before deploying.
Does upgrading Next.js help with memory errors?
Often it does, because recent versions added memory optimizations and fixed known leaks, but test the upgrade on a branch first since major versions change behavior.
Can Thomas Osayi fix a Next.js build that keeps running out of memory?
Yes. Thomas Osayi fixes one blocking bug such as a failing build from $60 in about 2 days through his Quick Fix package.
Sources
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- Environment variables undefined on Vercel? Every cause and fix
- How to fix a CORS error on a Next.js API route
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service