ERROR: Could not resolve "crypto"
I'm using HonoJS with the library Hono Sessions. When I use wrangler to preview
wrangler pages dev dist
I get an error complaining about not being able to resolve crypto:
For context, I've ensured I have node compatibility enabled:
If I remove any imports of the Hono Sessions library my worker builds...the issue is if I go to that project and run their Cloudflare test, it works totally fine!
The only thing I can think of is they are testing against Cloudflare Workers, not Pages. Is there a difference in the node APIs offered between Workers and Pages?
If not, any thoughts on why this might be happening? If it matters I'm using Vite to build my project.
https://github.com/jcs224/hono_sessionsGitHub
GitHub - jcs224/hono_sessions: Cookie-based sessions for Hono appli...
Cookie-based sessions for Hono applications. Contribute to jcs224/hono_sessions development by creating an account on GitHub.
3 Replies
Hey, did you manage to find a solution to this, I'm facing a similar issue regarding crypto with nextjs?
Howdy! The answer is "kinda" but I'm not sure it'll be very helpful. I was able trace the issue in this library, Hono Sessions, is with nanoid v4, which uses the Node Crypto API. Switching to nanoid v5 uses the Web Crypto API which is more universally available and resolves the proble (i'm like 95% sure it does; I was able to get it working but then I broke something with local npm module imports and just haven't had time to go dig).
It is still really confusing to me as to why Wrangler/Cloudflare don't respect calls to the node crypto API if you have the nodjs_compat enabled. It might have something to do with with the way Vite handles the references to the node crypto API, but I'm not sure
Thanks a lot :awesome: