10 days ago
I'm experiencing out-of-memory errors during the Next.js build phase on my Railway project (TheLedgerLink). The build
consistently fails after ~60 seconds with the error: "FATAL ERROR: Reached heap limit Allocation failed - JavaScript
heap out of memory" even with NODE_OPTIONS='--max-old-space-size=7168' (7GB heap).
Project specs:
- Next.js 16.2.6 with Webpack bundler
- 4,262 npm packages
- Heavy AI/ML dependencies (Hugging Face, TensorFlow, ONNX Runtime, Claude SDK, etc.)
- Deployed as standalone output
Optimizations already implemented:
- Disabled PWA feature (~50MB savings)
- Aggressive Webpack code-splitting (150KB max chunk size, dedicated ML/AI bundles)
- Tree-shaking enabled
- Removed unnecessary dependencies
The 8GB builder environment is insufficient for this project's dependency graph. I'd like to request an increase to
10GB+ of available builder memory.
Could you please increase the memory allocation for my project or suggest an alternative deployment option?
1 Replies
10 days ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open Railway • 10 days ago
10 days ago
I would not treat this only as a request for a bigger builder.
NODE_OPTIONS=--max-old-space-size=7168 is probably too high for an 8 GB build environment. That setting only caps V8 old-space. The build still needs memory for Node native allocations, SWC, webpack, package-manager work, child processes, the OS, and file cache. With a 7 GB old-space cap, the process can run the builder out of headroom before garbage collection has enough room to recover.
I would test this first:
NODE_OPTIONS=--max-old-space-size=4096
NEXT_TELEMETRY_DISABLED=1If that gets farther but still fails, try 5120. I would not set old-space close to the full machine limit.
The code splitting you listed mostly helps the runtime bundle. It does not necessarily reduce build-time memory if the AI/ML packages are still reachable from the Next.js app graph. With Hugging Face, TensorFlow, ONNX Runtime, and SDK packages in the same app, the important check is whether any file imported by app/, pages/, layout, route handlers, or client components imports the AI stack directly or indirectly.
I would split the dependency graph like this:
- Keep the Next.js service focused on UI, routing, auth, and API calls.
- Move model, embedding, inference, and document-processing code into a separate worker or API service.
- In the Next.js app, import only a small HTTP client or typed interface for that worker.
- Make sure no client component, shared layout, or common
lib/*helper imports the AI modules indirectly. - Run
npm ls @tensorflow/tfjs onnxruntime-nodeplus the package-manager equivalent for your Hugging Face packages, then remove anything that is not needed by the web build.
For Railway, I would use one of these paths:
- Reduce builder pressure inside Railway:
NODE_OPTIONS=--max-old-space-size=4096
NEXT_TELEMETRY_DISABLED=1Then rebuild after isolating the AI/ML imports from the web app graph.
- Split the app into two services:
web: Next.js standalone app
worker/api: AI, ML, embeddings, document processingThe web build should not need to install or compile the whole AI stack.
If the web app truly needs more than the Railway builder can provide, build outside Railway in CI with a larger runner, publish a Docker image, and have Railway deploy that image. Railway's own build docs mention building your own image and deploying it directly as the option when build behavior or cache requirements need more control.
If the build succeeds with a lower old-space cap after the AI/ML imports are isolated, the issue was builder headroom and dependency graph pressure, not just an absolute memory quota problem. If it still fails after the split, then a larger external build runner or prebuilt Docker image is the cleaner option than trying to force the whole monolith through the Railway builder.
Status changed to Awaiting User Response Railway • 10 days ago
3 days ago
This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!
Status changed to Solved Railway • 3 days ago