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

Cannot read properties of undefined (reading 'sqrt') #415

Closed
aliberkyurtoglu opened this issue Mar 27, 2022 · 37 comments
Closed

Cannot read properties of undefined (reading 'sqrt') #415

aliberkyurtoglu opened this issue Mar 27, 2022 · 37 comments

Comments

@aliberkyurtoglu
Copy link

aliberkyurtoglu commented Mar 27, 2022

We have been using it in our project for 3-4 months. But for the last 2 days, we have been experiencing problems in our application due to this error. What could be the problem? If anyone has encountered this problem before, please help. @evanvosberg

Package Version: 4.1.1

Node Version: 14.15.4

NextJS Version: 12.1.0

React Version: 17.0.2

Error

Screen Shot 2022-03-27 at 14 54 49

hashHelper.ts

Screen Shot 2022-03-27 at 14 58 45

@stevez86
Copy link

I'm getting the same error. I'm using the library to hash a persisted query for apollo client. Only happens on "production" in the browser while also using nextjs 12.1.2.

Screen Shot 2022-03-29 at 4 37 36 PM

Screen Shot 2022-03-29 at 4 37 28 PM

@stevez86
Copy link

@aliberkyurtoglu I was able to resolve this issue by disabling "swcMinify" in next.config and by adjusting "next-transpile-modules" to resolveSymlinks: false. Check each one separately, I'm still working out which was the exact cause.

@luckyyyyy
Copy link

@kdy1 Same problem in version 1.3.96

@kdy1
Copy link

kdy1 commented Nov 13, 2023

@luckyyyyy 1.3.96? Can you provide a minimal reproduction, or file an issue on https://github.com/swc-project/swc ?

@nassimbenkirane
Copy link

@kdy1 Same error happening to production build, when upgrading from next@14.0.1 to next@14.0.2

@kdy1
Copy link

kdy1 commented Nov 13, 2023

@nassimbenkirane Can you provide a minimal reproduction?

@luckyyyyy
Copy link

@luckyyyyy 1.3.96? Can you provide a minimal reproduction, or file an issue on https://github.com/swc-project/swc ?

let me try

@luckyyyyy
Copy link

Sorry, I'm a bit busy. Here's a simple example of using webpack to compress with SWC and include Crypto-js, and then viewing the result with npx serve dist.
@kdy1

web.zip

@nassimbenkirane
Copy link

nassimbenkirane commented Nov 13, 2023

Oh I didn't see that you were doing the same @luckyyyyy
Here is a sandbox with a repro
https://codesandbox.io/p/sandbox/keen-carlos-n79pkz

Step 1: open the drawer
Capture d’écran 2023-11-13 à 21 22 18

Step 2: click "Start Server 3000" under the + button
Capture d’écran 2023-11-13 à 21 23 16

Step 3: Open debugger and open console
Capture d’écran 2023-11-13 à 21 25 49

See the error
Capture d’écran 2023-11-13 à 21 26 29

@dattenlagiday
Copy link

dattenlagiday commented Nov 13, 2023

same error Cannot read properties of undefined (reading 'sqrt') using nextjs 14.0.2
Error on browser:
image

Bundle analyze:
image

Updated: Downgrade crypto-js from @4.2.0 to @4.1.1 solve the problem

@aaronabf
Copy link

Same issue with Next.js 14.0.2

@vakkiee
Copy link

vakkiee commented Nov 15, 2023

same error Cannot read properties of undefined (reading 'sqrt') using nextjs 14.0.2 Error on browser: image

Bundle analyze: image

Updated: Downgrade crypto-js from @4.2.0 to @4.1.1 solve the problem

Even in that version, it still results in an error.
This issue has not been resolved yet.

"next": "^14.0.2",
"crypto-js": "^4.1.1",

Only happens on "production"

TypeError: Cannot read properties of undefined (reading 'sqrt')

Your cooperation is greatly appreciated.

@yhrebeniuk
Copy link

I've started to get the same error after upgrading to next 14.
Setting swcMinify: false in next.config.js helped, as explained here.

@kdy1
Copy link

kdy1 commented Nov 15, 2023

Can you assign this to me?

@Tarbez
Copy link

Tarbez commented Nov 15, 2023

I've started to get the same error after upgrading to next 14. Setting swcMinify: false in next.config.js helped, as explained here.

I've faced the same issue, swcMinify: false is not an option for me since it breaks the entire build on production.
Thinking about rolling back to 13... Works in "next": "^13.5.5", with "eslint-config-next": "^13.5.4",

kdy1 added a commit to swc-project/swc that referenced this issue Nov 16, 2023
**Description:**

Regarding vercel/next.js#55682, I made a mistake while investigating. I assumed it's `swcMinify`-only but it seems like `node-fetch` is fundamentally incompatible with our minification options.

I thought `node-fetch` works with terser, but it did not, and it **should not**. But as I thought `terser` works, I assumed that `terser` has a hack for `AbortSignal`. And that's how I fixed `swcMinify: true`. The correct fix is `keep_classnames: true` but it will result in +~10% bundle size bloat.
At first, I used the correct way (`keep_classnames: true`), but it resulted in the bundle size bloat, so I mimicked the hack of terser with vercel/next.js#57904.

While working on other minification issues, I found that there's no such hack in `terser` and I need to remove the hack in the SWC minifier.
The repro in vercel/next.js#55682 does not work with `swcMinify: false`, even with next@14. The problem of minifier and node-fetch was not exploited before `serverMinification: true`.


**Related issue:**

 - brix/crypto-js#415
@null-prophet
Copy link

same issue that randomly appeared today for me on a yarn upgraded repo.

import AES from 'crypto-js/aes'
import enc from 'crypto-js/enc-utf8'

// then call like so
const value = AES.decrypt(data, input).toString(enc)

Changed my imports to this to decode some code in Next js and its now production building and rendering.

I think something weird is happening with the globally scoped imports and Next JS bundling. latest v14. Strangely it was working with V14 and 4.1.1 version for a while but I did a global yarn install/refresh of state (using berry) and it then happened to start using the 4.2.0 release.

I lost 4 hrs of my life debugging this and there is 0 errors on the Typescript side or local dev to show this issue only cryptic errors if you do a local build then start to test locally a prod build.

This will get it working until you guys figure out how to get your packaging compliant with the bundlers next js v14 are using.

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

import AES from 'crypto-js/aes'
import enc from 'crypto-js/enc-utf8'

// then call like so
const value = AES.decrypt(data, input).toString(enc)

Changed my imports to this to decode some code in Next js and its now production building and rendering.

I tried that, but it didn't work..
It works in the development environment(npm run dev), but it does not work well in the production build.

---version
"crypto-js": "^4.2.0",
"next": "^14.0.2",

スクリーンショット 2023-11-17 9 57 11 スクリーンショット 2023-11-17 9 57 27

@null-prophet
Copy link

I made sure I cleaned the .next folders each time I ran build then start to be sure.

This is the joys of the transient dependencies and version problems you get in npm/yarn projects. Do you have multiple crypto js versions loaded through other libs or just 4.2.0?

Sorry its not working for you, I had to comment everything out, add in bit by bit until it started to work again.

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

I am following the steps below.

1. Delete .next and node_modules
2. npm run build (default command)
3. npm run start (default command)

If I comment out the encrypted part, it works fine, so it seems that there is a problem with the encrypted part.
It doesn't work not only in v4.2.0 but also in v4.1.1.

@null-prophet
Copy link

I am following the steps below.

1. Delete .next and node_modules
2. npm run build (default command)
3. npm run start (default command)

If I comment out the encrypted part, it works fine, so it seems that there is a problem with the encrypted part. It doesn't work not only in v4.2.0 but also in v4.1.1.

Can you share a snippet of the code that your using inc the imports etc? Try and see what might be causing it to fail?

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

Can you share a snippet of the code that your using inc the imports etc? Try and see what might be causing it to fail?

encrypt

import AES from "crypto-js/aes";

AES.encrypt(
              target,
              cryptrKey
            ).toString(),

decrypt

import AES from 'crypto-js/aes'
import enc from 'crypto-js/enc-utf8'

AES.decrypt(target, cryptrKey).toString(enc)

@null-prophet
Copy link

it was working for decrypt for me, sorry I can't help more. I do some occassional encrypt and it worked here as well.

its worth checking your node_modules and doing a npm ls --all > deps.txt and checking out what might be depending on the crypto lib or causing some issues.

Otherwise go back to v13 next. not a great solution but the only one to get you moving forward.

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

@null-prophet
[deps.txt]

@null-prophet
Copy link

null-prophet commented Nov 17, 2023

To me that seems ok, you have only one version depedency on 4.2.0 and the types are fine.

If it works in V13 next I would just revert, I have no idea why it worked for me and not you. Perhaps the dev team will have a solution soon.

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

@null-prophet
Thank you for your cooperation

It works fine with Next.js v13.
The current solution seems to be to lower the version.

@null-prophet
Copy link

no problem, delete away.

@vakkiee
Copy link

vakkiee commented Nov 17, 2023

@null-prophet
Thank you

@TVYDev
Copy link

TVYDev commented Nov 18, 2023

Next.js 14.0.1 => 🟢 Work for both crypto-js 4.1.1 and 4.2.0

Next.js 14.0.2 up => 🔴 Not Work for both crypto-js 4.1.1 and 4.2.0

✓ Linting and checking validity of types    
   ▲ Next.js 14.0.3
   - Environments: .env.local

✓ Creating an optimized production build    
✓ Compiled successfully
   Collecting page data  ..TypeError: Cannot read properties of undefined (reading 'sqrt')

@kdy1
Copy link

kdy1 commented Nov 18, 2023

vercel/next.js#58517 will fix it. (Waiting for vercel/turborepo#6472 to get merged first)

@JesseOgunlaja
Copy link

JesseOgunlaja commented Nov 26, 2023

Frustrating I'm getting the same error. image Don't know if it's also causing hydration error. Works temporarily after deleting .next, building and starting local production server for first few navigations.

Spent way too long trying to find the problem just to find it had to do with Next JS hopefully a later version of Next will have it all working.

Edit:
Transpile package worked for me when using 14.0.4

@dylandalal-asu
Copy link

Also have the same error. Tried to downgrade to next 13 to get back a stable build and now I am exceeding the maximum call stack size

@adentdk
Copy link

adentdk commented Dec 4, 2023

i'm using next js 14.0.3 and crypto-js 4.2.0, i used this configuration, and solved my problem

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['crypto-js'],
};

module.exports = nextConfig;

https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages

@kdy1
Copy link

kdy1 commented Dec 4, 2023

Is this still an issue with the next canary? (v14.0.4-canary.37)
If so, can someone provide a repro?

@drobotdaniil
Copy link

Hey @kdy1,
can verify that with v14.0.4-canary.37 issue disappears.

Just tested it right now.
Thank you.

@Careerawesome
Copy link

transpilePackages: ['crypto-js'],

this worked for me thanks

@kazuki-sf
Copy link

transpilePackages: ['crypto-js'],
This worked for me too! Thanks!

@Intangible-Media
Copy link

i'm using next js 14.0.3 and crypto-js 4.2.0, i used this configuration, and solved my problem

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['crypto-js'],
};

module.exports = nextConfig;

https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages

This solved my issue I was using pdfMake and this made it work again. THANK YOU!

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