cloudflare / workers-sdk

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

🐛 BUG: vitest unable resolve "url" module #6214

Open elct9620 opened 2 months ago

elct9620 commented 2 months ago

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

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

3.63.1 [Wrangler], 0.4.9 [@cloudflare/vitest-pool-workers], 1.5.3 [vitest]

What version of Node are you using?

20.12.2

What operating system and version are you using?

Mac Sonoma 14.4.1

Describe the Bug

Observed behavior

When running npm run test with vitest the error is displayed on the console.

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "Users/elct9620/Workspace/StarPortal/line-bot-testing-lab/node_modules/axios/lib/platform/node/classes/url".
  imported from "Users/elct9620/Workspace/StarPortal/line-bot-testing-lab/node_modules/axios/lib/platform/node/classes/URLSearchParams.js"

Use wrangler deploy and works correctly in the CloudFlare

Expected behavior

The test should passed.

Steps to reproduce

Please provide the following:

  1. npm init cloudflare
    • Enable TypeScript
    • Enable git
    • Don't deploy
  2. npm i -D axios
  3. Add import axios from 'axios' to the header in src/index.ts
  4. Add axios.get('https://example.com') to the handler in src/index.ts

The axios is required by SDK (https://github.com/line/line-bot-sdk-nodejs) everything is default from the template.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

workerd/server/server.c++:2835: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=%2Fprivate%2Ftmp%2Fcf-axios%2Fnode_modules%2Faxios%2Flib%2Fplatform%2Fnode%2Fclasses%2Furl&referrer=%2Fprivate%2Ftmp%2Fcf-axios%2Fnode_modules%2Faxios%2Flib%2Fplatform%2Fnode%2Fclasses%2FURLSearchParams.js&rawSpecifier=url
 ❯ test/index.spec.ts (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "private/tmp/cf-axios/node_modules/axios/lib/platform/node/classes/url".
  imported from "private/tmp/cf-axios/node_modules/axios/lib/platform/node/classes/URLSearchParams.js"
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  no tests
   Start at  18:23:16
   Duration  0ms

 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit
itsmechlark commented 1 month ago

Is there a workaround to resolve this issue?

zodo commented 1 month ago

Faced the same issue with langchain js. Vitest fails, wrangler dev works fine

Error: No such module "./node_modules/@langchain/core/dist/runnables/uuid". imported from "./node_modules/@langchain/core/dist/runnables/base.js"

andyjessop commented 1 month ago

Thanks very much for reporting this. I've reproduced the bug and added the issue to our backlog for work.

For anyone who ends up picking up this ticket, what seems to be happening here is that workerd cannot resolve the module, so it is requesting it from our "fallback service", whose job it is to find that module and return it. This could, therefore, be a bug in the node-like module resolution in the fallback service.

dereknex commented 2 weeks ago

is there any update? I have the same issue.

Using vars defined in .dev.vars
[vpw:inf] Starting single runtime for vitest.config.mts...
workerd/server/server.c++:2835: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=%2FUsers%2Fderek%2FWorkspaces%2Fagent%2Fnode_modules%2Fuuid%2Fdist%2Fesm-browser%2Findex.js&referrer=%2FUsers%2Fderek%2FWorkspaces%2Fagent%2Fnode_modules%2F%40langchain%2Fcore%2Fdist%2Frunnables%2Fbase.js&rawSpecifier=uuid