Repository: next.js
Issue: `npm run build` gets stuck after `FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory`
Labels: bug

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
> next info
    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:28 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T8110
    Binaries:
      Node: 18.12.0
      npm: 9.6.2
      Yarn: N/A
      pnpm: 7.14.0
    Relevant packages:
      next: 13.4.5-canary.6
      eslint-config-next: 13.4.4
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3
```


### Which area(s) of Next.js are affected? (leave empty if unsure)

_No response_

### Link to the code that reproduces this issue or a replay of the bug

https://github.com/juanlarrambebere/nextjs-memory-issue

### To Reproduce

1. Create a nextjs app with typescript
`npx create-next-app@latest`

2. Reduce Node memory allocation to a minimum (128mb) or less
`export NODE_OPTIONS=--max-old-space-size=128`

3. Build the app
`npm run build`

```
➜ my-app (main) ✔ export NODE_OPTIONS=--max-old-space-size=128
➜ my-app (main) ✔ npm run build

> my-app@0.1.0 build
> next build

- info Linting and checking validity of types ..
<--- Last few GCs --->

[83034:0x148040000]      952 ms: Mark-sweep (reduce) 127.0 (133.2) -> 126.5 (131.2) MB, 17.5 / 0.0 ms  (+ 2.1 ms in 8 steps since start of marking, biggest step 1.8 ms, walltime since start of marking 22 ms) (average mu = 0.368, current mu = 0.334) finali[83034:0x148040000]      978 ms: Mark-sweep (reduce) 127.2 (131.2) -> 126.9 (131.7) MB, 16.8 / 0.0 ms  (+ 2.0 ms in 8 steps since start of marking, biggest step 1.7 ms, walltime since start of marking 21 ms) (average mu = 0.325, current mu = 0.271) finali

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0x102982200 node::Abort() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 2: 0x1029823f0 node::ModifyCodeGenerationFromStrings(v8::Local<v8::Context>, v8::Local<v8::Value>, bool) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 3: 0x102ac8880 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 4: 0x102c735e8 v8::internal::EmbedderStackStateScope::EmbedderStackStateScope(v8::internal::Heap*, v8::internal::EmbedderStackStateScope::Origin, cppgc::EmbedderStackState) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 5: 0x102c720cc v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 6: 0x102c70764 v8::internal::Heap::HandleGCRequest() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 7: 0x102c1d604 v8::internal::StackGuard::HandleInterrupts() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 8: 0x102fdc494 v8::internal::Runtime_StackGuardWithGap(int, unsigned long*, v8::internal::Isolate*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
 9: 0x10332904c Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
10: 0x1032b4d20 Builtins_BaselineOutOfLinePrologue [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
11: 0x107d09178
12: 0x107d16320
13: 0x107b9bd88
14: 0x107b142cc
15: 0x107d17a20
16: 0x107d28ffc
17: 0x107de3c60
18: 0x107b9b8e0
19: 0x107d4fc80
20: 0x107b14304
21: 0x107d17a20
22: 0x107d34960
23: 0x107b9b8e0
24: 0x107b142cc
25: 0x107d39b68
26: 0x107b9b8e0
27: 0x107d4fc80
28: 0x107b14304
29: 0x107b94c64
30: 0x107d17a20
31: 0x107d0dcbc
32: 0x107b9ba18
33: 0x107d4f350
34: 0x107b14304
35: 0x107d0c91c
36: 0x107d28060
37: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
38: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
39: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
40: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
41: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
42: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
- info Linting and checking validity of types ...43: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
44: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
45: 0x1032b4198 Builtins_InterpreterEntryTrampoline [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
46: 0x1032e5ef4 Builtins_AsyncFunctionAwaitResolveClosure [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
47: 0x1033746f8 Builtins_PromiseFulfillReactionJob [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
48: 0x1032d7c4c Builtins_RunMicrotasks [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
49: 0x1032b23a4 Builtins_JSRunMicrotasksEntry [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
50: 0x102bf4e10 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
51: 0x102bf5300 v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
52: 0x102bf54dc v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*, v8::internal::MaybeHandle<v8::internal::Object>*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
53: 0x102c1bbac v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
54: 0x102c1c444 v8::internal::MicrotaskQueue::PerformCheckpoint(v8::Isolate*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
55: 0x1028ccc4c node::InternalCallbackScope::Close() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
56: 0x1028cc7c4 node::InternalCallbackScope::~InternalCallbackScope() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
57: 0x102985530 node::fs::FileHandle::CloseReq::Resolve() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
58: 0x10299b01c node::fs::FileHandle::ClosePromise()::$_0::__invoke(uv_fs_s*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
59: 0x10297e7b0 node::MakeLibuvRequestCallback<uv_fs_s, void (*)(uv_fs_s*)>::Wrapper(uv_fs_s*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
60: 0x103290cbc uv__work_done [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
61: 0x103294458 uv__async_io [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
62: 0x1032a61a4 uv__io_poll [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
63: 0x1032948e8 uv_run [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
64: 0x1028cd6d4 node::SpinEventLoop(node::Environment*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
65: 0x1029bd128 node::NodeMainInstance::Run() [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
66: 0x102950ec8 node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResult const*) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
67: 0x102951190 node::Start(int, char**) [/Users/juanlarrambebere/.nvm/versions/node/v18.12.0/bin/node]
68: 0x10786508c
- info Linting and checking validity of types ...
```

### Describe the Bug

Building the app gets stuck if the process runs of memory.

Sometimes when building the app the memory required by nodejs exceeds its memory allocation capacity (1.5gb since nodejs 12). When this happens the build gets stuck at `info Linting and checking validity of types` instead of failing. This is particularly bad in the CI environment because actions gets stuck or even worse, the step gets marked as successful when it should be marked as error.

In the reproduction steps I drastically reduced nodejs's memory allocation capacity to a bear minimum so we can realiably reproduce it with an "empty" nextjs application, though if the app is big enough, the actual limit (1.5gb) might be reached.

### Expected Behavior

If the process runs out of memory the build should fail instead of getting stuck.

### Which browser are you using? (if relevant)

_No response_

### How are you deploying your application? (if relevant)

_No response_