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

Cloudflare: The requested module 'node:crypto' does not provide an export named 'constants' #659

Closed
2 tasks done
thdxr opened this issue Apr 7, 2024 · 5 comments
Closed
2 tasks done
Labels
tooling issue A tooling issue outside of the library, e.g. transpilers, bundlers, etc not supporting exports yet

Comments

@thdxr
Copy link

thdxr commented Apr 7, 2024

What happened?

it seems like jose depends on constants from node:crypto which is not yet available in cloudflare workers.

is it possible i'm packaging it wrong?

Version

5.2.1

Runtime

Cloudflare Workers

Runtime Details

workers

Code to reproduce

This isn't easily shared, want to have a discussion first and then can dig into recreations

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct
@thdxr thdxr added the triage label Apr 7, 2024
@panva
Copy link
Owner

panva commented Apr 7, 2024

is it possible i'm packaging it wrong?

Quite possibly. Every bundler worth its salt knows to target the Web API / Browser build for workerd.

I'll add an explicit target just to be sure but you should ask in workerd / bundler of your choice on why a node.js target is used over a browser one.

@panva panva closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
@panva panva added tooling issue A tooling issue outside of the library, e.g. transpilers, bundlers, etc not supporting exports yet and removed triage labels Apr 7, 2024
@FraBle
Copy link

FraBle commented May 6, 2024

I'm running into the same issue. @thdxr, how did you resolve it?

I'm trying to add a new hono middleware for propelauth based on the clerk-auth middleware. @propelauth/node uses jose under the hood and when I try to run wrangler dev it throws the same error:

> wrangler dev src/index.ts

 ⛅️ wrangler 3.53.1
-------------------
Using vars defined in .dev.vars
Your worker has access to the following bindings:
- Vars:
  - APP_ENV: "dev"
  - SENTRY_DSN: "(hidden)"
[wrangler:inf] Ready on http://localhost:51665
⎔ Starting local server...
✘ [ERROR] service core:user:my-app-api: Uncaught SyntaxError: The requested module 'node:crypto' does not provide an export named 'constants'

Edit: I think I have a hunch. I had compatibility_flags = [ "nodejs_compat" ] set in my wrangler.toml. Removing it starts throwing other errors now, but I'm wondering whether the compatibility flag causes jose to run in node mode instead of worker/browser.

Edit2: How does jose determine its runtime (browser vs node)?

@panva
Copy link
Owner

panva commented May 6, 2024

Edit2: How does jose determine its runtime (browser vs node)?

jose doesn't. The runtime/bundler does. All jose does is conditional export hints in

jose/package.json

Lines 79 to 86 in fd46e2c

"types": "./dist/types/index.d.ts",
"bun": "./dist/browser/index.js",
"deno": "./dist/browser/index.js",
"browser": "./dist/browser/index.js",
"worker": "./dist/browser/index.js",
"workerd": "./dist/browser/index.js",
"import": "./dist/node/esm/index.js",
"require": "./dist/node/cjs/index.js"

It would appear the nodejs_compat flag makes your bundle process target the nodejs conditional export for which the underlying the node:crypto (and other) builtins are incomplete in CF Workers.

@FraBle
Copy link

FraBle commented May 7, 2024

@panva I found the proposal that introduced those runtime keys. Still, I couldn't find a source on how those conditional export hints impact the bundling within Cloudflare's wrangler (with/without the node compatibility flag). Would you happen to have any pointers?

@panva
Copy link
Owner

panva commented May 7, 2024

I would not. You need to ask your bundler or stop using node compat if you want to depend on modules whos compat level is not sufficient.

I've added a warning to #265

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
tooling issue A tooling issue outside of the library, e.g. transpilers, bundlers, etc not supporting exports yet
Projects
None yet
Development

No branches or pull requests

3 participants