cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.44k stars 614 forks source link

🐛 BUG: "Module cannot be synchronously required" when using `@cloudflare/vitest-pool-workers` #5367

Open nvie opened 3 months ago

nvie commented 3 months ago

Which Cloudflare product(s) does this pertain to?

Miniflare, Workers Vitest Integration

What version(s) of the tool(s) are you using?

wrangler@3.37.0, @cloudflare/vitest-pool-workers@0.1.5, vitest@1.3.0

What version of Node are you using?

v18.19.1

What operating system and version are you using?

Mac Sonoma 14.2.1

Describe the Bug

Observed behavior

After switching to using @cloudflare/vitest-pool-workers in the vitest.config.ts file, we're seeing CJS/ESM errors that were not there before. My minimal replication doesn't even use any Miniflare/Wrangler bits, but shows how just changing the defineConfig to defineWorkersConfig breaks the tests.

Expected behavior

The simple test would still pass, despite now using the defineWorkersConfig call.

Steps to reproduce

  1. Check out my minimal replication on this branch
  2. Check out the second-to-last commit
  3. Run npm run test, notice it passes without using the pool workers wrapper it works :)
  4. Now check out the last commit, which only changes the pool wrapper
  5. Run npm run test, notice the tests now fail due to a CJS/ESM issue that wasn't there before it no longer works :(

Please provide a link to a minimal reproduction

https://github.com/nvie/clean-project/tree/vitest-jsonwebtoken

Please provide any relevant error logs

 FAIL  test/bug.test.ts [ test/bug.test.ts ]
Error: Module cannot be synchronously required while it is being instantiated or evaluated. This error typically means that a CommonJS or NodeJS-Compat type module has a circular dependency on itself, and that a synchronous require() is being called while the module is being loaded.
 ❯ Users/nvie/Projects/clean-project/node_modules/semver/classes/range.js?mf_vitest_no_cjs_esm_shim:205:20
 ❯ Users/nvie/Projects/clean-project/node_modules/semver/classes/comparator.js?mf_vitest_no_cjs_esm_shim:141:15
 ❯ Users/nvie/Projects/clean-project/node_modules/semver/index.js?mf_vitest_no_cjs_esm_shim:29:20
 ❯ Users/nvie/Projects/clean-project/node_modules/jsonwebtoken/lib/asymmetricKeyDetailsSupported.js?mf_vitest_no_cjs_esm_shim:1:16
 ❯ Users/nvie/Projects/clean-project/node_modules/jsonwebtoken/lib/validateAsymmetricKey.js?mf_vitest_no_cjs_esm_shim:1:42
 ❯ Users/nvie/Projects/clean-project/node_modules/jsonwebtoken/verify.js?mf_vitest_no_cjs_esm_shim:6:31
 ❯ Users/nvie/Projects/clean-project/node_modules/jsonwebtoken/index.js?mf_vitest_no_cjs_esm_shim:3:11
nvie commented 3 months ago

Another, maybe similar, CJS/ESM bug happens when importing from itty-router-extras@0.4.5.

Simply adding:

import { error } from "itty-router-extras" // @0.4.5

will break the test with the following error:

FAIL  test/bug.test.ts [ test/bug.test.ts ]
Error: No such module "Users/nvie/Projects/clean-project/node_modules/itty-router-extras/middleware".
 ❯ Users/nvie/Projects/clean-project/node_modules/itty-router-extras/index.js?mf_vitest_no_cjs_esm_shim:1:20
nvie commented 3 months ago

We worked around this by no longer using these packages, and it's no longer an issue for us.

JordanFaust commented 3 months ago

Is there an actual resolution to this problem other than not using the dependencies? Have the same problem for sermver. Should this issue actually be closed?

ahmadbilaldev commented 2 months ago

Facing the same issue. Is there a solution?

FlowFlorent commented 1 month ago

Getting a similar issue when using the svix package. Should this issue be reopened to hopefully find a solution?

Note: issue still present on 0.4.3

Error: Module cannot be synchronously required while it is being instantiated or evaluated. This error typically means that a CommonJS or NodeJS-Compat type module has a circular dependency on itself, and that a synchronous require() is being called while the module is being loaded.
 ❯ Users/florentlefebvre/dev/liveblocks-cloudflare/node_modules/svix/dist/openapi/http/isomorphic-fetch.js?mf_vitest_no_cjs_esm_shim:13:16
 ❯ Users/florentlefebvre/dev/liveblocks-cloudflare/node_modules/svix/dist/openapi/http/http.js?mf_vitest_no_cjs_esm_shim:25:14
 ❯ Users/florentlefebvre/dev/liveblocks-cloudflare/node_modules/svix/dist/openapi/index.js?mf_vitest_no_cjs_esm_shim:13:14
 ❯ Users/florentlefebvre/dev/liveblocks-cloudflare/node_modules/svix/dist/index.js?mf_vitest_no_cjs_esm_shim:14:17
esdee commented 1 week ago

I'm seeing this issue with Kysely as a dependency in my project. Was wondering if there's been any further news on this.

ychua commented 1 day ago

This issue needs to be look into.

Vitest is the recommended testing framework and this issue is stopping us from using the worker with AWS SDKs, to be specific, awssns.

These days it is common to intetegrate worker with AWS-sdks or sementic versioning, I strongly suggest the worker-sdk team to review the priority of this bug again