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 use Cloudflare Durable Objects with Hono and Vite #190

Open
jhlabs opened this issue Nov 1, 2024 · 6 comments
Open

Cannot use Cloudflare Durable Objects with Hono and Vite #190

jhlabs opened this issue Nov 1, 2024 · 6 comments

Comments

@jhlabs
Copy link

jhlabs commented Nov 1, 2024

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!

@jhlabs
Copy link
Author

jhlabs commented Nov 9, 2024

@yusukebe any idea how this could be resolved?

@jmorrell
Copy link

This is also an issue with Workflows

@yusukebe
Copy link
Member

This is not 100% Hono-matter, but it is a problem to use the custom namespace cloudflare:workers within Vite. I can't find any solution now.

@jhlabs
Copy link
Author

jhlabs commented Nov 21, 2024

I am able to get around the vite error by excluding it from the bundle like so:

// vite.config.ts
import build from "@hono/vite-build/cloudflare-workers";
import devServer from "@hono/vite-dev-server";
import cloudflareAdapter from "@hono/vite-dev-server/cloudflare";
import { defineConfig } from "vite";

export default defineConfig(({ mode }) => {
	if (mode === "client") {
		return {
			build: {
				rollupOptions: {
          external: ["cloudflare:workers"],          
					input: ["./src/client.ts"],
					output: {
						entryFileNames: "static/client.js",
						chunkFileNames: "static/assets/[name]-[hash].js",
						assetFileNames: "static/assets/[name].[ext]",
					},
				},
				emptyOutDir: false,
				copyPublicDir: false,
			},
		};
	}

	return {
		build: {
			minify: true,
			rollupOptions: {
				output: {
					entryFileNames: "_worker.js",
				},
				external: ["cloudflare:workers"],
			},
		},
		plugins: [
			devServer({
				adapter: cloudflareAdapter,
				entry: "./src/index.tsx",
			}),
			build(),
		],
	};
});

However, I then get a new error:

service core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER: Worker "core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER"'s binding "UserActor" refers to a service "core:user:worker", but no such service is defined.
8:49:10 AM [vite] Internal server error: The Workers runtime failed to start. There is likely additional logging output above.
      at #assembleAndUpdateConfig (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:9980:13)
      at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
      at async Mutex.runWith (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:3632:16)
      at async #waitForReady (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10037:5)
      at async Miniflare2._getProxyClient (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10161:5)
      at async Miniflare2.getBindings (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10180:25)
      at async getPlatformProxy (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0/node_modules/wrangler/wrangler-dist/cli.js:217062:20)
      at async cloudflareAdapter (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/adapter/cloudflare.js:6:13)
      at async getAdapterFromOptions (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/dev-server.js:132:15)
      at async getRequestListener.overrideGlobalObjects (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/dev-server.js:68:31)
service core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER: Worker "core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER"'s binding "UserActor" refers to a service "core:user:worker", but no such service is defined.
8:49:15 AM [vite] Internal server error: The Workers runtime failed to start. There is likely additional logging output above.
      at #assembleAndUpdateConfig (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:9980:13)
      at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
      at async Mutex.runWith (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:3632:16)
      at async #waitForReady (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10037:5)
      at async Miniflare2._getProxyClient (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10161:5)
      at async Miniflare2.getBindings (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/miniflare@3.20241022.0/node_modules/miniflare/dist/src/index.js:10180:25)
      at async getPlatformProxy (/Users/johannes/dev/sublime-workers/node_modules/.pnpm/wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0/node_modules/wrangler/wrangler-dist/cli.js:217062:20)
      at async cloudflareAdapter (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/adapter/cloudflare.js:6:13)
      at async getAdapterFromOptions (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/dev-server.js:132:15)
      at async getRequestListener.overrideGlobalObjects (file:///Users/johannes/dev/sublime-workers/node_modules/.pnpm/@hono+vite-dev-server@0.16.0_hono@4.6.7_miniflare@3.20241022.0_wrangler@3.83.0_@cloudflare+workers-types@4.20241022.0_/node_modules/@hono/vite-dev-server/dist/dev-server.js:68:31)

@Code-Hex
Copy link

Same issue: #24 (comment)

@Code-Hex
Copy link

This comment is very helpful to me.
I just added script_name = "worker-name" to my wrangler.toml, then I confirmed to work it.
cloudflare/workers-sdk#6988 (comment)

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

4 participants