When working with a Next.js
API route, I start by importing webcrypto like this:
import { webcrypto } from 'crypto'
Later on, I utilize it in the following manner:
const random = webcrypto.getRandomValues(new Uint8Array(8))
While testing on localhost
everything works smoothly. However, upon deploying to production (on Vercel), an error crops up:
ERROR [next-auth][error][SIGNIN_EMAIL_ERROR]
https://next-auth.js.org/errors#signin_email_error Cannot read property 'getRandomValues' of undefined {
error: {
message: "Cannot read property 'getRandomValues' of undefined",
stack: "TypeError: Cannot read property 'getRandomValues' of undefined\n" +
' at Object.generateVerificationToken (/var/task/.next/server/chunks/696.js:97:78)\n' +
' at email (/var/task/node_modules/next-auth/core/lib/email/signin.js:22:188)\n' +
' at Object.signin (/var/task/node_modules/next-auth/core/routes/signin.js:117:50)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
' at async NextAuthHandler (/var/task/node_modules/next-auth/core/index.js:238:26)\n' +
' at async NextAuthNextHandler (/var/task/node_modules/next-auth/next/index.js:23:19)\n' +
' at async /var/task/node_modules/next-auth/next/index.js:59:32\n' +
' at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:182:9)\n' +
' at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:386:9)\n' +
' at async Object.fn (/var/task/node_modules/next/dist/server/base-server.js:488:37)',
name: 'TypeError'
},
providerId: 'email',
message: "Cannot read property 'getRandomValues' of undefined"
}
The crux of the issue lies in this detail:
message: "Cannot read property 'getRandomValues' of undefined"
Why does this functionality fail upon deployment, yet perform flawlessly on localhost
? How can I rectify this discrepancy?
It's worth noting that the deployment employs https