Use node import for 'crypto' to ease Cloudflare deploy with Vite - #7
Conversation
kentcdodds
left a comment
There was a problem hiding this comment.
I typically prefer my node imports to be like this anyway so that works out nicely! I'm glad to hear this makes cloudflare support easier as well. Thanks!
|
🎉 This PR is included in version 1.1.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
@mw10013 I am working on a Remix + Vite + Cloudflare Pages app and am running into issues with Buffer not being defined:
I have the @kentcdodds This package has worked perfectly in development for implementing my own SMS OTP authentication, hope I can get the Buffer issue sorted. Thank you for this! |
|
I'm not familiar with the limitations of that platform, but I welcome a PR to fix this if you find out what to do about it. |
https://github.com/dev-xo/remix-auth-totp/blob/main/docs/customization.md#cloudflare Vite "scripts": {
"start": "wrangler pages dev ./build/client --compatibility-flags=nodejs_compat"
}Ensure the Buffer global is set up before using remix-auth-totp. import { Buffer } from 'node:buffer'
function setUpGlobals() {
globalThis.Buffer = Buffer
} |
Use node import for 'crypto' node builtin module to ease Cloudflare deploy with Vite.
Cloudflare offers some node.js compatibility if you switch it on and use the
node:prefix in your imports. See: https://developers.cloudflare.com/workers/runtime-apis/nodejs/This eases Remix deployment with the Vite compiler since no additional Vite configuration is needed. Otherwise, you may need to specify resolve.alias's and srr.noExternal's, which can get tricky if
totpis a dependency of a dependency as is the case forremix-auth-totp.Note that version 3 of
remix-auth-totpwill include a similar change. (dev-xo/remix-auth-totp#45)