Repository: next.js
Issue: Production build crashes on Next version >= 14.0.2 when CryptoJS module is used on frontend
Labels: bug

### Link to the code that reproduces this issue

[Code sandbox example](https://codesandbox.io/p/sandbox/affectionate-orla-jz2wfg?file=%2Fnext.config.js%3A1%2C1&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clp3r06iq00083b6kqq4zln1p%2522%252C%2522sizes%2522%253A%255B70%252C30%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clp3r06iq00033b6kts4kh8zx%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clp3r06iq00063b6kcjrhm2o6%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clp3r06iq00073b6k3619z4gd%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B64.8757411%252C35.1242589%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clp3r06iq00033b6kts4kh8zx%2522%253A%257B%2522id%2522%253A%2522clp3r06iq00033b6kts4kh8zx%2522%252C%2522activeTabId%2522%253A%2522clp3rdvpx02vj3b6koxm24f74%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fapp%252Fpage.tsx%2522%252C%2522id%2522%253A%2522clp3rdvpx02vj3b6koxm24f74%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fservices%252Fcrypto-service.ts%2522%252C%2522id%2522%253A%2522clp3rsgyy05iv3b6kzgza97hz%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fcomponents%252Fclient.tsx%2522%252C%2522id%2522%253A%2522clp3ru1fm05o63b6kowzr1wss%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fpackage.json%2522%252C%2522id%2522%253A%2522clp3s1pav07we3b6krygz8hxt%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%257D%252C%2522clp3r06iq00073b6k3619z4gd%2522%253A%257B%2522id%2522%253A%2522clp3r06iq00073b6k3619z4gd%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522SYSTEM_METRICS%2522%252C%2522id%2522%253A%2522clp3rgp4p03663b6kcv7xcec3%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%252C%257B%2522type%2522%253A%2522TASK_PORT%2522%252C%2522taskId%2522%253A%2522start%2522%252C%2522port%2522%253A3000%252C%2522id%2522%253A%2522clp3ri6xu03vx3b6k1gh6wuwc%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%252F%2522%257D%255D%252C%2522activeTabId%2522%253A%2522clp3ri6xu03vx3b6k1gh6wuwc%2522%257D%252C%2522clp3r06iq00063b6kcjrhm2o6%2522%253A%257B%2522id%2522%253A%2522clp3r06iq00063b6kcjrhm2o6%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522build%2522%252C%2522id%2522%253A%2522clp3rw4fa06b23b6kk3y0rlmo%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522start%2522%252C%2522id%2522%253A%2522clp3rwmbr06ed3b6kao3imisz%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%252C%257B%2522id%2522%253A%2522clp3s20th083t3b6kdqoqb7xx%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TERMINAL%2522%252C%2522shellId%2522%253A%2522clp3s20wt003ze0gl1op2byzu%2522%257D%255D%252C%2522activeTabId%2522%253A%2522clp3rwmbr06ed3b6kao3imisz%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Atrue%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D)

### To Reproduce

1. Run dev to make sure the app works
2. Run build 
3. Run start - error will occur

### Current vs. Expected behavior

Hello, Next community, 

I wanted to report a bug that was introduced in Next v14.0.2. The error reported on the frontend is: 
> Application error: a client-side exception has occurred (see the browser console for more information).

When opening the console there's an uncaught Promise error on calling `Math.sqrt` function. Tbh, I have no idea what could cause this issue. Because of that, I've attached a video recording of my production build, hoping a more experienced NextJs engineer could tell me more about why this is happening. 

Since this information provides little, to no information, and after reading the text above, you would probably say something like "Dude, I wish you all the luck of this world to solve this, but I have no f*cking idea what the hell is going on.", I've decided to test a couple of canary releases to reduce the search space to the smallest amount possible. 

Running on the latest canary release at the time of writing ([14.0.4-canary.3](https://www.npmjs.com/package/next/v/14.0.4-canary.3)) still has this issue. So I've done an interpolation search on the 14.0.2 canaries:

1. [14.0.2-canary.1](https://www.npmjs.com/package/next/v/14.0.2-canary.1)       ✅
2. [14.0.2-canary.10](https://www.npmjs.com/package/next/v/14.0.2-canary.10)  ✅
3. [14.0.2-canary.20](https://www.npmjs.com/package/next/v/14.0.2-canary.20) ❌
4. [14.0.2-canary.15](https://www.npmjs.com/package/next/v/14.0.2-canary.15)  ✅
5. [14.0.2-canary.18](https://www.npmjs.com/package/next/v/14.0.2-canary.18)  ✅
6. **[14.0.2-canary.19](https://www.npmjs.com/package/next/v/14.0.2-canary.19)  ❌ 👈**

By looking at the [release notes](https://github.com/vercel/next.js/releases/tag/v14.0.2-canary.19) of 14.0.2-canary.19 
I think one of these PRs could cause this issue:
* https://github.com/vercel/next.js/pull/58130 
* https://github.com/vercel/next.js/pull/57908

I hope this small research of mine helped, and that it will be fixed soon. Thank you 🙏 
For now, I'm running my app on v14.0.1 without any issues.

### Edit
The issue is related to [CryptoJS](https://www.npmjs.com/package/crypto-js) module being used on front end. 


https://github.com/vercel/next.js/assets/24474083/8efbeb87-3e16-4304-9d9f-fa6464fd6566

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.2-canary.19
  eslint-config-next: 14.0.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

App Router

### Additional context

_No response_