Repository: next.js
Issue: Next js build issue, FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
Labels: bug,Script (next/script)

### Link to the code that reproduces this issue

https://github.com/Elham13/nabhigator/tree/temp

### To Reproduce

1- Clone the repo from temp branch
2- Run npm install
3- Run npm run build

### Current vs. Expected behavior

**Current**
` Creating an optimized production build ...

<--- Last few GCs --->

[20876:000001BB9964D840]   102237 ms: Mark-sweep (reduce) 1883.5 (1970.4) -> 1883.5 (1969.9) MB, 1472.9 / 0.0 ms  (average mu = 0.245, current mu = 0.000) last resort; GC in old space requested
[20876:000001BB9964D840]   104329 ms: Mark-sweep (reduce) 1883.5 (1969.9) -> 1883.5 (1969.9) MB, 2092.6 / 0.0 ms  (average mu = 0.110, current mu = 0.000) last resort; GC in old space requested


<--- JS stacktrace --->

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: 00007FF77617194F node_api_throw_syntax_error+175855
 2: 00007FF7760F6026 SSL_get_quiet_shutdown+59654
 3: 00007FF7760F7D10 SSL_get_quiet_shutdown+67056
 4: 00007FF776BA21F4 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF776B8D582 v8::Isolate::Exit+674
 6: 00007FF776A2407E v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath+4030
 7: 00007FF776A2BA4F v8::internal::Factory::AllocateRaw+783
 8: 00007FF776A422FA v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithFiller+90
 9: 00007FF776A425E3 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithMap+35
10: 00007FF776800898 v8::internal::OrderedNameDictionary::Add<v8::internal::LocalIsolate>+712
11: 00007FF776800E77 v8::internal::OrderedNameDictionary::FindEntry<v8::internal::LocalIsolate>+279
12: 00007FF776800CE4 v8::internal::OrderedHashTable<v8::internal::OrderedHashMap,2>::EnsureGrowable<v8::internal::Isolate>+100
13: 00007FF77672CC79 v8::internal::CompilationCache::IsEnabledScriptAndEval+4905
14: 00007FF776C3F751 v8::internal::SetupIsolateDelegate::SetupHeap+558193
15: 00007FF776C1375B v8::internal::SetupIsolateDelegate::SetupHeap+377979

> Build error occurred
Error: Jest worker encountered 1 child process exceptions, exceeding retry limit
    at ChildProcessWorker.initialize (D:\BPaasProjects\nabhigator\node_modules\next\dist\compiled\jest-worker\index.js:1:11580)
    at ChildProcessWorker._onExit (D:\BPaasProjects\nabhigator\node_modules\next\dist\compiled\jest-worker\index.js:1:12545)
    at ChildProcess.emit (node:events:529:35)
    at ChildProcess._handle.onexit (node:internal/child_process:292:12) {
  type: 'WorkerError'
}
PS D:\BPaasProjects\nabhigator> npm run build --max-old-space-size=4000

> nabhigator@0.1.0 build
> next build

  ▲ Next.js 14.2.3
  - Environments: .env

   Creating an optimized production build ...

<--- Last few GCs --->

[13492:0000023DE8C74750]    89698 ms: Mark-sweep (reduce) 1888.9 (1996.7) -> 1888.9 (1996.4) MB, 1172.1 / 0.0 ms  (average mu = 0.210, current mu = 0.000) last resort; GC in old space requested
[13492:0000023DE8C74750]    90990 ms: Mark-sweep (reduce) 1888.9 (1996.4) -> 1888.9 (1996.4) MB, 1292.0 / 0.0 ms  (average mu = 0.115, current mu = 0.000) last resort; GC in old space requested


<--- JS stacktrace --->

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: 00007FF77617194F node_api_throw_syntax_error+175855
 2: 00007FF7760F6026 SSL_get_quiet_shutdown+59654
 3: 00007FF7760F7D10 SSL_get_quiet_shutdown+67056
 4: 00007FF776BA21F4 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF776B8D582 v8::Isolate::Exit+674
 6: 00007FF776A2407E v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath+4030
 7: 00007FF776A2BA4F v8::internal::Factory::AllocateRaw+783
 8: 00007FF776A422FA v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithFiller+90
 9: 00007FF776A425E3 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithMap+35
10: 00007FF776800898 v8::internal::OrderedNameDictionary::Add<v8::internal::LocalIsolate>+712
11: 00007FF776800E77 v8::internal::OrderedNameDictionary::FindEntry<v8::internal::LocalIsolate>+279
12: 00007FF776800CE4 v8::internal::OrderedHashTable<v8::internal::OrderedHashMap,2>::EnsureGrowable<v8::internal::Isolate>+100
13: 00007FF77672CC79 v8::internal::CompilationCache::IsEnabledScriptAndEval+4905
14: 00007FF776C3F751 v8::internal::SetupIsolateDelegate::SetupHeap+558193
15: 00007FF776C1375B v8::internal::SetupIsolateDelegate::SetupHeap+377979
PS D:\BPaasProjects\nabhigator> npm run build --max-old-space-size=6144

> nabhigator@0.1.0 build
> next build

  ▲ Next.js 14.2.3
  - Environments: .env

   Creating an optimized production build ...

<--- Last few GCs --->

[23512:000001E041826240]    81282 ms: Mark-sweep (reduce) 1889.0 (2012.8) -> 1889.0 (2012.8) MB, 1378.1 / 0.0 ms  (average mu = 0.195, current mu = 0.000) last resort; GC in old space requested
[23512:000001E041826240]    82567 ms: Mark-sweep (reduce) 1889.0 (2012.8) -> 1889.0 (2012.8) MB, 1284.4 / 0.0 ms  (average mu = 0.109, current mu = 0.000) last resort; GC in old space requested


<--- JS stacktrace --->

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: 00007FF77617194F node_api_throw_syntax_error+175855
 2: 00007FF7760F6026 SSL_get_quiet_shutdown+59654
 3: 00007FF7760F7D10 SSL_get_quiet_shutdown+67056
 4: 00007FF776BA21F4 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF776B8D582 v8::Isolate::Exit+674
 6: 00007FF776A2407E v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath+4030
 7: 00007FF776A2BA4F v8::internal::Factory::AllocateRaw+783
 8: 00007FF776A422FA v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithFiller+90
 9: 00007FF776A425E3 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithMap+35
10: 00007FF776800898 v8::internal::OrderedNameDictionary::Add<v8::internal::LocalIsolate>+712
11: 00007FF776800E77 v8::internal::OrderedNameDictionary::FindEntry<v8::internal::LocalIsolate>+279
12: 00007FF776800CE4 v8::internal::OrderedHashTable<v8::internal::OrderedHashMap,2>::EnsureGrowable<v8::internal::Isolate>+100
13: 00007FF77672CC79 v8::internal::CompilationCache::IsEnabledScriptAndEval+4905
14: 00007FF776C3F751 v8::internal::SetupIsolateDelegate::SetupHeap+558193
15: 00007FF776C1375B v8::internal::SetupIsolateDelegate::SetupHeap+377979`

**Expected**
To build successfully

### Provide environment information

```bash
Operating System 
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro

Binaries: 
  Node: v18.18.0
  npm: 9.8.1

Relevant Packages
  next: 14.2.3
  react: ^18
  typescript: ^5
```


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

Script (next/script)

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

next build (local)

### Additional context

If anyone can help me find the reason of memory leak issue during build, I'd be grateful.
Note: Please don't suggest me to run npm run build --max-old-space-size=6144, I want to find the code or the place where it's causing this issue.
Thanks