MasterKale / SimpleWebAuthn

WebAuthn, Simplified. A collection of TypeScript-first libraries for simpler WebAuthn integration. Supports modern browsers, Node, Deno, and more.
https://simplewebauthn.dev
MIT License
1.63k stars 137 forks source link

A Node.js module is loaded ('crypto') which is not supported in the Edge Runtime. #601

Closed ayushghatkar8080 closed 2 months ago

ayushghatkar8080 commented 2 months ago

Describe the issue

Hello there when i was building my webapp with next-auth i got some error regarding @simplewebauthn/server """

./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/getWebCrypto.js A Node.js module is loaded ('crypto' at line 46) which is not supported in the Edge Runtime. Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

Import trace for requested module: ./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/getWebCrypto.js ./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/verifyEC2.js ./node_modules/@simplewebauthn/server/esm/metadata/verifyJWT.js ./node_modules/@simplewebauthn/server/esm/services/metadataService.js ./node_modules/@simplewebauthn/server/esm/index.js

"""

Reproduction Steps

1.write a basic nextjs project 2.make a basic simplewebauthn passkey system 3.build command with nextjs

  1. u got it

Expected behavior

i thing your code is not compatible with vercel/jextjs's new edge time

Dependencies

SimpleWebAuthn Libraries

"@simplewebauthn/browser": "^9.0.1", "@simplewebauthn/server": "^9.0.3",

Output from Nextjs

appname@0.1.0 test cross-env NODE_ENV=localbuild && next build --debug && next start --port 80

▲ Next.js 14.2.5

./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/getWebCrypto.js A Node.js module is loaded ('crypto' at line 46) which is not supported in the Edge Runtime. Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

Import trace for requested module: ./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/getWebCrypto.js ./node_modules/@simplewebauthn/server/esm/helpers/iso/isoCrypto/verifyEC2.js ./node_modules/@simplewebauthn/server/esm/metadata/verifyJWT.js ./node_modules/@simplewebauthn/server/esm/services/metadataService.js ./node_modules/@simplewebauthn/server/esm/index.js ./node_modules/next-auth/node_modules/@auth/core/providers/webauthn.js ./node_modules/next-auth/node_modules/@auth/core/providers/passkey.js ./node_modules/next-auth/providers/passkey.js ./auth.ts

info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules ✓ Linting and checking validity of types
✓ Collecting page data ✓ Generating static pages (20/20) ✓ Collecting build traces ✓ Finalizing page optimization

Route (app) Size First Load JS ┌ ○ / 6.79 kB 200 kB ├ ƒ /api/auth/[...nextauth] 0 B 0 B ├ ○ /api/auth/error 187 B 190 kB


+ First Load JS shared by all            188 kB
  ├ chunks/396464d2-b3585379bdc09c30.js  35.9 kB
  ├ chunks/5636-1b4e30b9c23b31e3.js      96 kB
  ├ chunks/fd9d1056-aadedd8f22b98db8.js  53.7 kB
  └ other shared chunks (total)          2.5 kB

ƒ Middleware                             164 kB

○  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand

  ▲ Next.js 14.2.5
  - Local:        http://localhost:80

 ✓ Starting...
 ✓ Ready in 1985ms
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
[auth][warn][experimental-webauthn] Read more: https://warnings.authjs.dev#experimental-webauthn
MasterKale commented 2 months ago

Hello @ayushghatkar8080, server v9.0.3 first tries to find the crypto module at globalThis.crypto before trying to import Node's crypto module. That error you're seeing is telling me that, for some reason, globalThis.crypto is not resolving within the Next.js Edge runtime. The Next.js docs say it should be available, though:

https://nextjs.org/docs/app/api-reference/edge

Can you check and see what the value of globalThis.crypto is in your code before trying to import SimpleWebAuthn?

ayushghatkar8080 commented 2 months ago

Thank you, As it was not the problem of webauthn