honojs / vite-plugins

Vite Plugins for Hono
https://hono.dev
130 stars 34 forks source link

Cannot use Cloudflare Durable Objects with Hono and Vite #190

Open jhlabs opened 6 days ago

jhlabs commented 6 days ago

I am trying to setup Hono with Vite and Cloudflare Durable Objects. I have cloned the Durable Objects example and added the Vite Dev Server from this repository according to the instructions. Here is a repository for this setup https://github.com/jhlabs/hono-vite-durable-objects.

When I run vite to start the dev server (same is true for build with vite) I get the following error:

[vite] Pre-transform error: Failed to load url cloudflare:workers (resolved id: cloudflare:workers) in /Users/johannes/dev/hono-vite-durable-objects/src/counter.ts. Does the file exist?
10:39:14 AM [vite] Error when evaluating SSR module /src/counter.ts: failed to import "cloudflare:workers"
|- Error: Cannot find module 'cloudflare:workers' imported from '/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts'
    at nodeImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:53047:19)
    at ssrImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52914:22)
    at eval (/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts:3:50)
    at instantiateModule (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52972:11)

10:39:14 AM [vite] Error when evaluating SSR module ./src/index.ts: failed to import "cloudflare:workers"
|- Error: Cannot find module 'cloudflare:workers' imported from '/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts'
    at nodeImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:53047:19)
    at ssrImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52914:22)
    at eval (/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts:3:50)
    at instantiateModule (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52972:11)

10:39:14 AM [vite] Internal server error: Cannot find module 'cloudflare:workers' imported from '/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts'
      at nodeImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:53047:19)
      at ssrImport (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52914:22)
      at eval (/Users/johannes/dev/hono-vite-durable-objects/src/counter.ts:3:50)
      at instantiateModule (file:///Users/johannes/dev/hono-vite-durable-objects/node_modules/.pnpm/vite@5.4.10_@types+node@22.8.6/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:52972:11)

I suspect that the cloudflare:workers import should be excluded from being bundled by vite, as this is later resolved by the wrangler runtime. However, I could not get this to work and would expect this Vite Plugin to support this feature out of the box.

Thanks for your help in resolving this!