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:
✘ [ERROR] 1 error(s) and 0 warning(s) when compiling Worker.


▲ [WARNING] Failed to bundle _worker.js. Error: Build failed with 1 error:

_worker.js:1:580: ERROR: Could not resolve "crypto"
at failureErrorWithLog (/Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1636:15)
at /Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1048:25
at /Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1512:9
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: [
{
detail: undefined,
id: '',
location: [Object],
notes: [Array],
pluginName: '',
text: 'Could not resolve "crypto"'
}
],
warnings: []
}
✘ [ERROR] 1 error(s) and 0 warning(s) when compiling Worker.


▲ [WARNING] Failed to bundle _worker.js. Error: Build failed with 1 error:

_worker.js:1:580: ERROR: Could not resolve "crypto"
at failureErrorWithLog (/Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1636:15)
at /Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1048:25
at /Users/anthony/dev/breeze/node_modules/esbuild/lib/main.js:1512:9
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: [
{
detail: undefined,
id: '',
location: [Object],
notes: [Array],
pluginName: '',
text: 'Could not resolve "crypto"'
}
],
warnings: []
}
For context, I've ensured I have node compatibility enabled:
// wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
// wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
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_sessions
GitHub
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
Coolio85
Coolio857mo ago
Hey, did you manage to find a solution to this, I'm facing a similar issue regarding crypto with nextjs?
misterpoppins
misterpoppinsOP7mo ago
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
Coolio85
Coolio857mo ago
Thanks a lot :awesome:
Want results from more Discord servers?
Add your server