Trouble deploying Auth.js with Sveltekit on Cloudflare Pages - `node:crypto`

Hi all! I've been having a fun time trying to debug a build issue for the past few days using Auth.js on a Cloudflare Pages project built using SvelteKit. The problem seems to stem from the use of node built-in modules, but even after enabling nodejs_compat, I'm facing errors related to the use of node:crypto. Could anyone shine some light on where to go about debugging this issue?
$ npx wrangler pages dev --d1=DB .sveltekit/cloudflare

[...]

✘ [ERROR] service core:user:dms-hgm: Uncaught SyntaxError: The requested module 'node:crypto' does not provide an export named 'constants'

at null.<anonymous>
(file:///workspaces/dms/.svelte-kit/output/server/chunks/hooks.server.js:7:257)


✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
$ npx wrangler pages dev --d1=DB .sveltekit/cloudflare

[...]

✘ [ERROR] service core:user:dms-hgm: Uncaught SyntaxError: The requested module 'node:crypto' does not provide an export named 'constants'

at null.<anonymous>
(file:///workspaces/dms/.svelte-kit/output/server/chunks/hooks.server.js:7:257)


✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
(When deployed to pages directly, the error is the same, minus the stack trace.) 3258fd10.dms-hgm.pages.dev Acc. ID: 84e974647453d280b13cd8438d22d3c3
4 Replies
Jack M
Jack M4w ago
Currently, the vite.config.ts is fairly stock:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
plugins: [
sveltekit()
],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
},
});
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
plugins: [
sveltekit()
],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
},
});
I've tried using resolve:{alias:{}} to try and polyfill it, but I'm not massively familiar with Vite, and that may be more of a sveltekit issue anyway. The wrangler.toml currently looks like this:
name = "dms-hgm"
pages_build_output_dir = ".svelte-kit/cloudflare"
compatibility_date = "2024-05-13"
compatibility_flags = [ "nodejs_compat", "nodejs_als" ]

[vars]
AUTH_MICROSOFT_ENTRA_ID = "<REDACTED>"
AUTH_MICROSOFT_ENTRA_TENANT_ID = "<REDACTED>"

[[d1_databases]]
database_id = "<REDACTED>"
binding = "DB"
database_name = "dms-hgm"
preview_database_id = "DB" # Required for Pages local development

[env.production]
compatibility_date = "2023-11-15"

[env.production.vars]
AUTH_MICROSOFT_ENTRA_ID = "<REDACTED>"
AUTH_MICROSOFT_ENTRA_TENANT_ID = "<REDACTED>"

[[env.production.d1_databases]]
database_id = "<REDACTED>"
binding = "DB"
database_name = "DB"
name = "dms-hgm"
pages_build_output_dir = ".svelte-kit/cloudflare"
compatibility_date = "2024-05-13"
compatibility_flags = [ "nodejs_compat", "nodejs_als" ]

[vars]
AUTH_MICROSOFT_ENTRA_ID = "<REDACTED>"
AUTH_MICROSOFT_ENTRA_TENANT_ID = "<REDACTED>"

[[d1_databases]]
database_id = "<REDACTED>"
binding = "DB"
database_name = "dms-hgm"
preview_database_id = "DB" # Required for Pages local development

[env.production]
compatibility_date = "2023-11-15"

[env.production.vars]
AUTH_MICROSOFT_ENTRA_ID = "<REDACTED>"
AUTH_MICROSOFT_ENTRA_TENANT_ID = "<REDACTED>"

[[env.production.d1_databases]]
database_id = "<REDACTED>"
binding = "DB"
database_name = "DB"
Ultrasanic
Ultrasanic3w ago
I've been trying to do the exact same thing but node:crypto seems to not implement constants unfortunately. https://developers.cloudflare.com/workers/runtime-apis/nodejs/crypto/
Cloudflare Docs
Crypto · Cloudflare Workers docs
To use Node.js APIs in your Worker, add the nodejs_compat compatibility flag to your wrangler.toml file.
Jack M
Jack M3w ago
Eventually I tracked down this issue from jose, which is a library used by auth.js, but it seems to just suggest disabling node compat... which breaks other things 😬
Jack M
Jack M2w ago
I was able to solve this by explicitly installing @auth/core. For any future searchers, related issue is https://github.com/nextauthjs/next-auth/issues/11184
GitHub
Getting "The requested module 'node:crypto' does not provide an exp...
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/b...