JavaScript heap out of memory
Node used up all the memory it’s allowed and crashed, usually during a large build or in code that keeps holding on to more and more data.
Also appears as
- FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
- FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
Prompt to fix it
My build crashes with "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory". Find what uses so much memory before raising any limits: check for large files imported into the bundle (JSON, data, images), broad globs such as a Tailwind content pattern that reaches node_modules, and scripts or server code that load everything into memory. Fix the cause first. If the build still legitimately needs more, set NODE_OPTIONS=--max-old-space-size with a value that fits the build machine, and tell me where you set it.
Paste into Layerwise or any AI coding tool.
What it means
Node gives JavaScript a fixed amount of memory, called the heap, and picks the limit at startup based on the machine. On a laptop with 16 GB of memory it’s about 4 GB, and on small build machines it’s much lower. When a program needs more, Node tries to free memory (the “Last few GCs” lines above the error), gives up, and crashes with exit code 134.
During a build, this usually means the job is bigger than it should be: a huge JSON file or dataset imported into the bundle, a Tailwind content pattern that scans node_modules, or source maps for a very large bundle. In a running server, it means something keeps growing, like a cache with no limit or a query that loads a whole table into memory.
Raising the limit with --max-old-space-size helps when the job truly needs more memory, such as type-checking a big project. Keep it below the machine’s real memory, or the operating system kills the process instead. If memory keeps climbing no matter the limit, the code has a leak or a runaway loop, and more memory only delays the crash.
Common causes
- The build bundles something huge, such as a large dataset imported as JSON or files pulled in by a broad glob.
- A Tailwind
contentpattern or a custom script scansnode_modules. - The build machine has less memory than your laptop, so a build that passes locally fails on CI or the host.
- Server code keeps data forever: an unbounded cache, an array that only grows, or a query that loads every row.
- A loop that never ends keeps creating new objects.
How to fix it
- Find which command crashed:
next build,vite build,tsc, a test run, or the server itself. - Look for large imports. Move big datasets out of the bundle into files the app fetches at runtime, or into the database.
- Narrow broad globs. In Tailwind v3, point
contentat your source files, such as./src/**/*.{js,ts,jsx,tsx}. - If the build truly needs more memory, set the environment variable
NODE_OPTIONSto--max-old-space-size=4096(in megabytes) in the build settings, below the machine’s total memory. - For a server that slowly fills up, cap cache sizes and paginate large queries.
Related errors
All errorsCommand "npm run build" exited with 1
Your build script failed on the host. This line only reports the failure; the actual error is printed earlier in the build log.
FUNCTION_INVOCATION_TIMEOUT
A Vercel Function ran past its time limit, so Vercel stopped it and returned a 504 in place of your response.
Maximum call stack size exceeded
A function kept calling itself, directly or through other functions, until JavaScript ran out of room to keep track of the calls.
Cannot find module
Node tried to load a package or file that isn’t where the code says it is, usually because it was never installed or the path is wrong.
Build with an agent that checks its work
The Layerwise agent clicks through the live preview and reads the console, so it catches errors before you do.