nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
23.27k stars 3.18k forks source link

Getting "The requested module 'node:crypto' does not provide an export named 'constants'" for basic SvelteKit project using Cloudflare Pages #11184

Open ChristianJacobsen opened 1 month ago

ChristianJacobsen commented 1 month ago

Environment

  System:
    OS: Linux 6.9 Arch Linux
    CPU: (32) x64 AMD Ryzen 9 3950X 16-Core Processor
    Memory: 113.16 GB / 125.74 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.3.0 - /usr/bin/node
    npm: 10.8.1 - /usr/bin/npm
    pnpm: 9.2.0 - /usr/bin/pnpm
  npmPackages:
    @auth/sveltekit: ^1.2.0 => 1.2.0 

Reproduction URL

https://github.com/ChristianJacobsen/sveltekit-auth-cloudflare-node-crypto-reproduce

Describe the issue

When I build my SvelteKit project and launch it with wrangler pages dev, I am told that The requested module 'node:crypto' does not provide an export named 'constants'. This is with the nodejs_compat compatibility flag enabled. Without that, it complains that the module can't be found altogether.

Now, I know that Cloudflare Workers doesn't provide the constants export of the Node.js Crypto API (https://developers.cloudflare.com/workers/runtime-apis/nodejs/crypto/), but as far as I can tell Auth.js isn't supposed to rely on anything but the browser's Web Crypto API. Is this not true for the Credentials provider I am using (for now)?

From the Edge Compatibility docs it says:

Edge compatibility is something Auth.js has optimized for. That means that you can run the core Auth.js functionality on any JavaScript runtime you choose. The key word here, however, being core functionality. If you use only Auth.js / next-auth and no other library in your Auth.js callbacks, Middleware, etc. then you can use it wherever you want!

I've assumed this far that everything in @auth/sveltekit is "core" to Auth.js, and I'm not using any external libraries in any portion of my auth code.

How to reproduce

Run pnpm run build and then pnpm run start in the linked repo.

Expected behavior

I expect a simple auth solution to work for SvelteKit + Cloudflare Pages.

jackmawer commented 3 weeks ago

I'm also having this issue without solution. When nodejs_compat is enabled, the dependency that works with JWTs, jose, breaks as it expects crypto.constants, which is unsupported by Cloudflare's compat layer. See https://github.com/panva/jose/issues/659. As for what is still depending on nodejs code when the compatibility layer is disabled, that's still an unknown to me.

ChristianJacobsen commented 3 weeks ago

@jackmawer, I don't know what happened to my other project, but it suddenly started working without upgrading Wrangler to a newer version... When I clone the reproduction repo I linked above I still get the error after install -> build -> wrangler pages dev.

Obviously something fishy, but for now I'm happy it "works".

jackmawer commented 3 weeks ago

@jackmawer, I don't know what happened to my other project, but it suddenly started working without upgrading Wrangler to a newer version... When I clone the reproduction repo I linked above I still get the error after install -> build -> wrangler pages dev.

Obviously something fishy, but for now I'm happy it "works".

Would you mind sharing your package lockfile? I wonder if it's an unusual dependency tree thing.

jackmawer commented 3 weeks ago

Although my understanding was that @auth/sveltekit was all that was needed to be imported, it turns out that explicitly defining @auth/core as a dependency seems to fix the building issues. Have not tried on your repro repo yet but it has worked on my local project.

ChristianJacobsen commented 3 weeks ago

Although my understanding was that @auth/sveltekit was all that was needed to be imported, it turns out that explicitly defining @auth/core as a dependency seems to fix the building issues. Have not tried on your repro repo yet but it has worked on my local project.

Interesting... I did add @auth/core as a dev-dependency because I wanted to augment some of the types.

I just tested with the repro-repo, and it does indeed fix all of the issues. Maybe Balázs has the answers we need as to why this is happening.

panva commented 3 weeks ago

cc @balazsorban44 ☝️ same as we discussed yesterday