tsndr / cloudflare-worker-jwt

A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
MIT License
649 stars 51 forks source link

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

Closed devinellis closed 6 months ago

devinellis commented 7 months ago

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:

vnphanquang commented 7 months ago

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 commented 7 months ago

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

tsndr commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

@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 commented 6 months ago

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 commented 6 months ago

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 :)

vnphanquang commented 6 months ago

@tsndr thank you. No worries I understand 😀

Eusebiotrigo commented 6 months ago

Thank you very much!!!