Next.js Discord

Discord Forum

Build Fail - FATAL ERROR: JavaScript heap out of memory

Answered
ShinmenTakezo posted this in #help-forum
Open in Discord
Hi All,

I've looked everywhere and just cannot find anything that helps me figure out how I can reduce the size of the package. Increasing the memory also does not appear to do anything at all.

Using Docker to deploy to GCP. Everything was working fine until I added some more Node Packages which increased the size and now I am getting this error during the build.

1.009 Attention: Next.js now collects completely anonymous telemetry regarding usage.
1.009 This information is used to shape Next.js' roadmap and prioritize features.
1.009 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
1.009 https://nextjs.org/telemetry
1.009 
1.041   ▲ Next.js 14.2.5
1.041   - Environments: .env.local, .env.production
1.042 
1.210    Creating an optimized production build ...
104.5 
104.5 <--- Last few GCs --->
104.5 
104.5 [17:0xffff87751630]   102237 ms: Mark-Compact 2008.5 (2082.0) -> 1992.8 (2082.0) MB, 772.80 / 0.00 ms  (average mu = 0.120, current mu = 0.023) allocation failure; scavenge might not succeed
104.5 [17:0xffff87751630]   103110 ms: Mark-Compact 2008.7 (2082.0) -> 1993.4 (2082.5) MB, 847.73 / 0.00 ms  (average mu = 0.074, current mu = 0.029) allocation failure; scavenge might not succeed
104.5 
104.5 
104.5 <--- JS stacktrace --->
104.5 
104.5 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
104.5 ----- Native stack trace -----
104.5 
105.1 npm error path /app
105.1 npm error command failed
105.1 npm error signal SIGABRT


ANY help would be appreciated and THANK YOU in advance.
Answered by Himalayan
Make sure the ENV NODE_OPTIONS line is above the build command since its there its crashing
RUN npm ci

ENV NODE_OPTIONS=--max_old_space_size=8192

RUN npm run build
View full answer

19 Replies

@ShinmenTakezo Hi All, I've looked everywhere and just cannot find anything that helps me figure out how I can reduce the size of the package. Increasing the memory also does not appear to do anything at all. Using Docker to deploy to GCP. Everything was working fine until I added some more Node Packages which increased the size and now I am getting this error during the build. 1.009 Attention: Next.js now collects completely anonymous telemetry regarding usage. 1.009 This information is used to shape Next.js' roadmap and prioritize features. 1.009 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: 1.009 https://nextjs.org/telemetry 1.009 1.041 ▲ Next.js 14.2.5 1.041 - Environments: .env.local, .env.production 1.042 1.210 Creating an optimized production build ... 104.5 104.5 <--- Last few GCs ---> 104.5 104.5 [17:0xffff87751630] 102237 ms: Mark-Compact 2008.5 (2082.0) -> 1992.8 (2082.0) MB, 772.80 / 0.00 ms (average mu = 0.120, current mu = 0.023) allocation failure; scavenge might not succeed 104.5 [17:0xffff87751630] 103110 ms: Mark-Compact 2008.7 (2082.0) -> 1993.4 (2082.5) MB, 847.73 / 0.00 ms (average mu = 0.074, current mu = 0.029) allocation failure; scavenge might not succeed 104.5 104.5 104.5 <--- JS stacktrace ---> 104.5 104.5 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 104.5 ----- Native stack trace ----- 104.5 105.1 npm error path /app 105.1 npm error command failed 105.1 npm error signal SIGABRT ANY help would be appreciated and THANK YOU in advance.
Just re-run
@James4u Just re-run
Thanks for the reply...not sure what you mean. I've attempted many times, same problem.
Irish Red and White Setter
For what it's worth, we run nextjs in prod and it crashes about every 12 hours.
Seems to use 4gb in our container by default on Azure. Managed to increase it to 8gb instead with:
node --max-old-space-size=8192

And that does NOT help the issue at all.

I'm guessing some form of memory leak, the memory usage increases steadily over time until crash.
node --max-old-space-size=8192 if this doesn't help you, sorry no idea from my end
Do you know how I would add this when using Docker? I've added this, which is what I assume I'd need but not working.

ENV NODE_OPTIONS="--max-old-space-size=8192"
I've also tried this one and still does not work.

CMD ["node", "--max-old-space-size=8192", "server.js"]
@Himalayan You added it before the build command right?
You mean in the package.json file?
Himalayan
In the dockerfile
The two items I added in the Docker file are:

ENV NODE_OPTIONS="--max-old-space-size=8192"

&

CMD ["node", "--max-old-space-size=8192", "server.js"]
You've actually given me a good idea however as I've never added the option within the package.json file command.
Himalayan
Make sure the ENV NODE_OPTIONS line is above the build command since its there its crashing
RUN npm ci

ENV NODE_OPTIONS=--max_old_space_size=8192

RUN npm run build
Answer
Himalayan
Thats properly why then
Let me give that a go!
Ha! Both worked!

Moving the ENV above the build command in Docker worked as well as adding the option directly in package.json file.
Thank you!!! @Himalayan