Could not resolve "node:crypto"
how can i use nodejs packages on the server side?
i see it says platform: node but i dont see this config in the dashboard
@CodingWithJamal
15 Replies
hi
hi. i see Presley is typing the answer (he built this 🙂 )
Convex can use two runtimes - Convex's runtime where queries, mutations and actions run and Node.js where you can run actions. You can use crypto in Convex's runtime but it is a global. See "Supported APIs" here https://docs.convex.dev/functions/runtimes.
If you need to use a node API not available in Convex's environment, you should use an action function and make it run in Node.js by adding "use node" at the top of the file.
okay thanks
@presley im still getting the same error
but
crypto should be built into the node runtime right?
there is no semicolon after "use node"
...my auto formatter adds that
but okay
btw if you don't want to use node, you can use the
crypto
global (remove the import line) which has a slightly different api https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_APIWeb Crypto API - Web APIs | MDN
The Web Crypto API is an interface allowing a script to use cryptographic primitives in order to build systems using cryptography.
oh ok thanks
oh i was wrong about the semicolon. the problem is you used single quotes
'use node';
but only double quotes are supported "use node";
. sorry for the confusion@CodingWithJamal actually the single vs double quotes nor the semi-column matters. Sorry about the confusion. I just tested with the following code and the deployed and ran fine from the dashboard .
oh..
I just finished rewriting for browser :haHAA:
yeah, this is fine too.
but thank you i will remember this
let me know if you ran into further issues but "use node" should work and we have tried to make all variants of that work 🙂
One clarification. This is not "for browser" per say, but for "Convex's runtime" that is similar to Cloudflare & co in that it supports most APIs but is not full Node.
understood, thanks for the help