Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ChristianJacobsen opened this issue Jun 17, 2024 · 6 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@ChristianJacobsen
Copy link

ChristianJacobsen commented Jun 17, 2024

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.

@ChristianJacobsen ChristianJacobsen added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jun 17, 2024
@jackmawer
Copy link

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 panva/jose#659.
As for what is still depending on nodejs code when the compatibility layer is disabled, that's still an unknown to me.

@ChristianJacobsen
Copy link
Author

@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
Copy link

@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
Copy link

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
Copy link
Author

ChristianJacobsen commented Jun 21, 2024

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
Copy link
Contributor

panva commented Jun 21, 2024

cc @balazsorban44 ☝️ same as we discussed yesterday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

3 participants