Could not resolve "crypto"
I am trying to have a function that generates a code and returns it to the react client.
but then i get this error:
how would i resolve this? i also feel as though there is a gap in my understanding of how convex is supposed to work .
4 Replies
you probably need to use an action and put "use node" at the top of the file?
@Bogdan Convex's default environment is more similar to the browser (or Cloudflare workers), so
crypto
is a global, which has a subset of what's available in the browser:
https://developer.mozilla.org/en-US/docs/Web/API/crypto_property
So you can remove the import line and the rest will hopefully work as intended.crypto global property - Web APIs | MDN
The global read-only crypto property returns the Crypto object associated to the global object. This object allows web pages access to certain cryptographic related services.
thanks for the help. who would have guessed the answer to my question was in the part of the docs i didnt read.
the global crypto doesnt have the randomBytes method although i prolly could have worked around it. going to leave the solution code if anyone else looks for this issue
thanks for the help
For posterity if you want to use the convex runtime, the workaround would be
crypto.getRandomValues
which is similar to node's randomBytes