ReferenceError: crypto is not defined
I have a javascript file which makes use of crypto. This file is used by both my Cloudflare Pages Functions and my Node preinstall script when running npm i. Locally, on my PC, both run fine but on Cloudflare Pages the preinstall script crashes with ReferenceError: crypto is not defined. If I add at the top of the file, the preinstall step will run fine but the Pages Function will fail to build with I do have the nodejs_compat flag enabled. Is there any way to not have to use since this isn't required when running the preinstall script on my PC?
5 Replies
nodejs_compat has modules under a node prefix, like node:crypto
That works but aren't there any downsides to using node:crypto instead of the provided one on a pages function?
Node Crypto and the Web Crypto global on Workers/Pages are different things - different functions, etc
oh yeah right but node:crypto has webcrypto
sth like
I was able to fix it by adding this at the top + replacing every occurrence of crypto with webcrypto on my script
How do I do this if crypto is one of the peer dependecies and not a direct dependencies?
Is there a better way to solve this?