I am getting an `crypto is not defined at secureR...
# javascript
a
I am getting an
crypto is not defined  at secureRandomUuid
when i try to use
Uuid.random()
in nodejs. Weird thing is it works fine locally (mac) but it fails on my server (ubuntu). I was using Uuid.random normally the past few days and I just got this issue now. I don't need to install crypto to use right?
e
what version of nodejs?
crypto
is only available in the default top-level namespace since v19, https://nodejs.org/en/blog/announcements/v19-release-announce#stable-webcrypto
a
let me check but that is odd. i havent changed nodejs version
wow u are right. my server has 18
so do i need node 19 to use it? any other alternatives w/o it?
e
it may work on 18 with
--experimental-global-webcrypto
j
You could also do like
globalThis.crypto = require('node:crypto').webcrypto
I think
a
Thanks for the help folks. Turns out I never ran
Uuid.random()
on my server (changed my app's logic so now some objects are created fresh on the server instead of being prebuilt) Ended up updating node using nvm (simplest method i think) and it works the same on both machines
e
This is yet another issue of multiplatform K/JS. And we got lucky Node.js has got a global
crypto
lol
🚫 1
e
node.js added it to match browsers. it's common to all modern js environments
e
Yeah but using globals is Node.js is discouraged usually. Plus you now have a version limitation, and v18 is still very common.
c
18 will be end of life in April, so time to move on.
e
KGP is also currently defaulting to Node 22 to build test and run. if you want to use a different version in production, you should aim to use the same in dev