Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS build always going out of heap memory when building #76704

Open
Falling12 opened this issue Mar 1, 2025 · 16 comments
Open

NextJS build always going out of heap memory when building #76704

Falling12 opened this issue Mar 1, 2025 · 16 comments

Comments

@Falling12
Copy link

Link to the code that reproduces this issue

https://github.com/Falling12/loyalty-backend

To Reproduce

  1. Download the repo
  2. Start a build
  3. Watch the app go out of memory, I also tried giving more memory to nodejs, but 8 gigs wasnt enough too

Current vs. Expected behavior

Whats happening:

  1. The app is building normally until this step: ✓ Generating static pages (18/18)
  2. Then its starting to fill the memory
  3. And then I got the issue: FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Excepted behavior:
The app should build normally

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8122
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 9.15.4
Relevant Packages:
  next: 15.1.7 // There is a newer version (15.2.0) available, upgrade recommended! 
  eslint-config-next: 15.1.7
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

I'm developing this not really large app for a restaurant but recently got this issue where I can't build the app. It can happen that my code is wrong somewhere but I don't really think this. I would be pleased if someone could look into my code aswell. (The build also failed with the same issue when deploying to Vercel)

Thank you for the help in advance!

@sinan2000
Copy link

I would advise you firstly to cancel that service account, delete from your repo and add to .gitignore so you can make a new one. Anyone can have access to your firestore, firebase auth or any kind of firebase service that you have right now for this project.

@Falling12
Copy link
Author

I would advise you firstly to cancel that service account, delete from your repo and add to .gitignore so you can make a new one. Anyone can have access to your firestore, firebase auth or any kind of firebase service that you have right now for this project.

Yeah, I know, but thats not a production environment. But I wall add it to gitignore. Thank you!

@ericmesmo

This comment has been minimized.

@Falling12
Copy link
Author

Did u find any solution?

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

Yes, I actually found a workaround, if you add the following experimental feature to the config it will use less RAM.

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
/* config options here */
experimental: {
esmExternals: false
},
};

export default nextConfig;

@ericmesmo
Copy link

ericmesmo commented Mar 4, 2025

@Falling12 i try your implementation but didn't work. My error occurs on checking type validity of the build part.

Thanks anyway

@nickythorne
Copy link

nickythorne commented Mar 7, 2025

This issue is also happening for me during the linting/type checking part. Fine on nextjs 14.

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.3.0
Relevant Packages:
  next: 15.2.1 // Latest available version is detected (15.2.1).
  eslint-config-next: 15.2.1
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.8.2
Next.js Config:
  output: standalone
   Linting and checking validity of types  ...
web:build: <--- Last few GCs --->
web:build:
web:build: [96606:0x158008000]    91105 ms: Mark-Compact 4044.4 (4136.4) -> 4027.5 (4135.1) MB, pooled: 2 MB, 1002.08 / 0.00 ms  (average mu = 0.066, current mu = 0.012) allocation failure; scavenge might not succeed
web:build: [96606:0x158008000]    92058 ms: Mark-Compact 4045.0 (4136.9) -> 4029.0 (4136.7) MB, pooled: 2 MB, 941.67 / 0.00 ms  (average mu = 0.040, current mu = 0.012) allocation failure; scavenge might not succeed
web:build:
web:build:
web:build: <--- JS stacktrace --->
web:build:
web:build: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
web:build: ----- Native stack trace -----
web:build:
web:build:  1: 0x1042cb1e4 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  2: 0x1044cafd8 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  3: 0x1046d8f30 v8::internal::Heap::stack() [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  4: 0x1046ef3a0 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1::operator()() const [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  5: 0x1046eec34 void heap::base::Stack::SetMarkerAndCallbackImpl<v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1>(heap::base::Stack*, void*, void const*) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  6: 0x104e84d54 PushAllRegistersAndIterateStack [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  7: 0x1046d70f0 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  8: 0x1046cb8b8 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build:  9: 0x1046cc0f0 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build: 10: 0x1046af410 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build: 11: 0x104acaae4 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build: 12: 0x104f8daf4 Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build: 13: 0x104fd9344 Builtins_FastNewFunctionContextFunction [/Users/nthorne/.nvm/versions/node/v22.14.0/bin/node]
web:build: 14: 0x10b908070
web:build: 15: 0x10bce51bc
web:build: 16: 0x10b1c853c

@ericmesmo
Copy link

@nickythorne I identified the solution for my case and the error was happening because I had installed different versions of zod for each of the packages I have in my monorepo.
What you can do is:

  1. Disable eslint during build and check if the error actually only occurs in the typescript validation:
const nextConfig: NextConfig = {
  //some other configuration
  eslint: {
    ignoreDuringBuilds: true,
  },
}
  1. If the error is actually occurring in the type checking, you can check the package.json of your packages and verify if zod or other libs are in the same version (in my case it was zod). If there are different versions, you can access the root directory of your monorepo and run the command:
pnpm up -r zod

The solution to my memory leak problem was this.

@nickythorne
Copy link

nickythorne commented Mar 7, 2025

@ericmesmo Thanks, I double checked the zod versions and they're aligned in the monorepo. Additionally setting eslint.ignoreDuringBuilds: true didn't fix for me, but setting typescript.ignoreBuildErrors: true does fix the issue. I'm on the latest typescript version [email protected] so this is a shame, but at least I have a work-around for now until its resolved.

Suspiciously there is this related open bug in the typescript repo: microsoft/TypeScript#60166. So it might not be a nextjs issue (for my case at least).

Thanks again for the insights!

EDIT: downgrading typescript to version 5.5.4 and removing typescript.ignoreBuildErrors: true fixes the issue for me, so this is confirmed as some sort of increased memory usage with newer versions of typescript.

@jotyy
Copy link

jotyy commented Mar 8, 2025

I have also been troubled by this problem for a week. Updating the TypeScript version is not work for me

@Falling12
Copy link
Author

I have also been troubled by this problem for a week. Updating the TypeScript version is not work for me

Have you tried disabling esmExternals in your next.config.ts? I have sent an example above, maybe it will work for you too.

@jotyy
Copy link

jotyy commented Mar 8, 2025

I have also been troubled by this problem for a week. Updating the TypeScript version is not work for me

Have you tried disabling esmExternals in your next.config.ts? I have sent an example above, maybe it will work for you too.

Yes, I set it, but it's not useful to me.

@Falling12
Copy link
Author

@jotyy Can you send me the error message you get, so I can see how much RAM it consumes? Because when I set this esmExternals I still have to set an environment variable for NodeJS to allow heap usage up to 4096MB RAM.

@nickythorne
Copy link

nickythorne commented Mar 8, 2025

I have also been troubled by this problem for a week. Updating the TypeScript version is not work for me

@jotyy I had to downgrade typescript in my case, not update.

@jotyy
Copy link

jotyy commented Mar 9, 2025

@jotyy Can you send me the error message you get, so I can see how much RAM it consumes? Because when I set this esmExternals I still have to set an environment variable for NodeJS to allow heap usage up to 4096MB RAM.

Thanks, my error is also OOM; setting --max-old-space-size to 8GB allows the packaging to succeed. The problem is only during the Docker build, which is really strange.

@Arctomachine
Copy link

I experience this issue too. It almost takes down production server with amount of memory it consumes. 2 ram + 3 swap barely handles it.

It happens during lint stage.

Previous version I had with normal usage had ts 5.7 internally. Current version uses 5.8.

So I assume problem is exactly in ts 5.8

Is there easy way to force it to use 5.7 for linting?

@Arctomachine
Copy link

Solution or workaround

const nextConfig: NextConfig = {
	typescript: {
		ignoreBuildErrors: true,
	},
}
"build": "tsc --noEmit && next build",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants