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

Cannot find module ... @tsndr/cloudflare-worker-jwt/utils #66

Closed
devinellis opened this issue Jan 27, 2024 · 9 comments
Closed

Cannot find module ... @tsndr/cloudflare-worker-jwt/utils #66

devinellis opened this issue Jan 27, 2024 · 9 comments

Comments

@devinellis
Copy link

FYI, builds on Cloudflare Pages started failing in the last few days:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/opt/buildhome/repo/node_modules/@tsndr/cloudflare-worker-jwt/utils' imported from /opt/buildhome/repo/node_modules/@tsndr/cloudflare-worker-jwt/index.js

My package.json was: @tsndr/cloudflare-worker-jwt": "^2.2.1. I removed the ^ and builds are working again. I also tried 2.4.3 but saw the same error when running locally.

In case it matters, I use the module format to import the library:

import jwt from '@tsndr/cloudflare-worker-jwt'

Deployment Log:

  • Success 10:40AM January 20, 2024 (^2.2.1)
  • Failed 5:42PM January 25, 2024 (^2.2.1)
  • Success 1:46PM January 27, 2024 (2.2.1)
@vnphanquang
Copy link

vnphanquang commented Jan 31, 2024

This started in 2.4.1 when src/utils.ts was introduced. The problem is that tsc outputs the import statement for utils module as:

// index.js
import { ... } from "./utils";

This is incompatible with Node's ESM algorithm, which requires explicit .js extension. A possible fix is to include .js in src/index.ts

// src/index.ts
import { ... } from "./utils.js"

tsc will keep .js in output. However, test cases will fail because jest throws a Cannot find module './utils.js' from 'src/index.ts' error.

Hopefully these info are helpful. I will keep experimenting when i have some more time.


For the record, I'm using svelte-kit@2.5.0, vite@5.0.12, and @sveltejs/adapter-cloudflare@4.1.0, with ESM syntax.

@tsndr
Copy link
Owner

tsndr commented Feb 1, 2024

I'm looking into this, sorry for the inconvenience.

@tsndr
Copy link
Owner

tsndr commented Feb 1, 2024

Hey guys, I've created a test project, but for me everything is working as expected:

https://github.com/tsndr/cloudflare-worker-jwt-test

@focus-at
Copy link

focus-at commented Feb 1, 2024

same, trying on localhost in nuxt: 3.4.3 project

Cannot find module '.../@tsndr/cloudflare-worker-jwt/utils' imported from .../@tsndr/cloudflare-worker-jwt/index.js

@vnphanquang
Copy link

vnphanquang commented Feb 2, 2024

@tsndr yeah, a bundler is smart enough to resolve it without problem. esbuild is used in the case of wrangler. The reported issue only happens when running with bare node.

Now I understand this is a bit unexpected because the package is targeted at cloudflare environment and not node. But in the context of some frameworks (sveltekit for me and nuxt for @focus-at, which build apps to compatible output for cloudflare), seems like sometimes the modules are evaluated with node at build time. I am not sure if this is really necessary and should or should not be fixed from the framework side, but having the package compatible with Node's ESM algorithm would definitely be convenient.

I created this repo that reproduces the issue, please see README for the steps.

Here is the full log:

node:internal/modules/esm/resolve:264
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/[...]/cloudflare-worker-jwt-esm-reproduction/node_modules/@tsndr/cloudflare-worker-jwt/utils' imported from /[...]/cloudflare-worker-jwt-esm-reproduction/node_modules/@tsndr/cloudflare-worker-jwt/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:917:10)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///[...]/cloudflare-worker-jwt-esm-reproduction/node_modules/@tsndr/cloudflare-worker-jwt/utils'
}

Node.js v20.11.0

@Eusebiotrigo
Copy link

Eusebiotrigo commented Feb 21, 2024

It is happening to us also when running tests in vitest.

Running workers with wrangler 3.28.4 (latest at this moment).
Running Node20.

@tsndr
Copy link
Owner

tsndr commented Feb 21, 2024

Hey guys, I've now added esbuild and released the change with v2.4.6, this should hopefully fix this issue.

@vnphanquang I really appreciate your effort with the PR (#67), but I've just switched over from js to ts a little while ago and would like to keep it that way. I think a bundler is the best way forward for now, but I'm also always open for feedback and suggestions :)

@tsndr tsndr closed this as completed Feb 21, 2024
@vnphanquang
Copy link

@tsndr thank you. No worries I understand 😀

@Eusebiotrigo
Copy link

Thank you very much!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants