FG
๐ŸŒ Web & Full-StackVercel

[Next 9] out of memory when building the app

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Bug report Describe the bug Moving an application from Next 8 to Next 9. When I run `next build` the process goes out of memory and it cannot build the application. FYI, it's an application with 20 routes more or less. To Reproduce That's hard to reproduce as I don't have any idea what went wrong. Next 8 compiles without issue but not Next9. Here's the stack trace. If you know how to get a more descriptive output, let me know and I will provide: [code block] Expected behavior It should build Screenshots If applicable, add screenshots to help explain your problem. System information - OS: macOS - Node: 10.13.0 (it will have to work with node 8.X too) - Version of Next.js: 9.0.1 Additional context Add any other context about the problem here.

Error Output

ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Increase Node.js Memory Limit for Next.js Build

Medium Risk

The error 'JavaScript heap out of memory' occurs when the Node.js process exceeds the default memory limit of 1.5GB. This can happen during the build process of larger applications, especially when moving from Next.js 8 to 9, which may have increased memory requirements due to new features and optimizations.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Increase Node.js Memory Limit

    Set the Node.js memory limit to a higher value to accommodate the build process. This can be done by using the `--max-old-space-size` flag when running the build command.

    bash
    NODE_OPTIONS='--max-old-space-size=4096' next build
  2. 2

    Upgrade Node.js Version

    Consider upgrading to a more recent version of Node.js (e.g., 12.x or later) as they have better memory management and performance improvements over 10.13.0. Ensure compatibility with your application.

    bash
    nvm install 12 && nvm use 12
  3. 3

    Optimize Build Configuration

    Review and optimize your Next.js build configuration. This may include reducing the number of routes, optimizing images, or lazy loading components to decrease memory usage during the build.

    javascript
    const withImages = require('next-images'); module.exports = withImages({ /* config options */ });
  4. 4

    Monitor Memory Usage

    Use Node.js memory profiling tools to monitor memory usage during the build process. This can help identify specific areas of the code that may be consuming excessive memory.

    bash
    node --inspect-brk node_modules/.bin/next build

Validation

After applying the changes, run the build command again. If the build completes successfully without the 'out of memory' error, the fix has worked. Additionally, monitor the memory usage during the build to ensure it remains within acceptable limits.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

nextjsreactssr