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

crypto.randomUUID is not a function #8820

Closed
0x1af2aec8f957 opened this issue Feb 8, 2023 · 6 comments
Closed

crypto.randomUUID is not a function #8820

0x1af2aec8f957 opened this issue Feb 8, 2023 · 6 comments

Comments

@0x1af2aec8f957
Copy link

In my project, the use scenario of crypto is not directly introduced through the import syntax. In some places, I directly use the window.crypto.randomUUID method. This method can work as expected in the Chrome console. After the parceljs build, it cannot work as expected. I guess this may be related to Node Emulation. Can I configure it through @parcel/transformer-js in package.json? I can't find relevant documents.

Error screenshot:

Error screenshot

@0x1af2aec8f957
Copy link
Author

Additional note: The same code will not have this problem if it is run through parse serve.

@awerlang
Copy link

awerlang commented Feb 8, 2023

Note that window.crypto.randomUUID is only available under HTTPS context: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID

parcel serve might be running under HTTPS while your app doesn't?

@0x1af2aec8f957
Copy link
Author

@awerlang It is indeed available in the context of HTTPS. However, HTTPS is not used in the parcel run mode, which is inconsistent with the expected effect.

Related screenshots:

image

@awerlang
Copy link

awerlang commented Feb 9, 2023

No idea why it behaves that way.

I think you have three options:

  1. Make sure your app only runs under HTTPS;
  2. Use import("crypto") syntax to get "node emulation";
  3. Bring your own polyfill for window.crypto.

@awerlang
Copy link

awerlang commented Feb 9, 2023

If parcel run adds a window.crypto polyfill that's not available in a production build, I'd mark this as a bug.

@mischnic
Copy link
Member

mischnic commented Feb 9, 2023

localhost is treated a secure origin, even without HTTPS.
For everything apart from localhost, you do need HTTPS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants