Skip to content
GitHubXDiscord

Vite

This guide shows how to initialize and deploy a Vite.js React TypeScript application to Cloudflare using Alchemy.

Start by creating a new Vite.js project with Alchemy:

Terminal window
bunx alchemy create my-react-app --template=vite
cd my-react-app

Authenticate once with your Cloudflare account:

Terminal window
bun wrangler login

Run the deploy script generated by the template:

Terminal window
bun run deploy

You’ll get the live URL of your Vite.js site:

Terminal window
{
url: "https://website.<your-account>.workers.dev",
}

Work locally using the dev script:

Terminal window
bun run dev

Clean up all Cloudflare resources created by this stack:

Terminal window
bun run destroy

Alchemy requires a locally set password to encrypt Secrets that are stored in state. Be sure to change this.

ALCHEMY_PASSWORD=change-me
/// <reference types="@types/node" />
import alchemy from "alchemy";
import { Vite } from "alchemy/cloudflare";
const app = await alchemy("my-react-app");
export const worker = await Vite("website", {
main: "./worker/index.ts",
command: "bun run build",
});
console.log({
url: worker.url,
});
await app.finalize();
// Auto-generated Cloudflare binding types.
// @see https://alchemy.run/concepts/bindings/#type-safe-bindings
import type { worker } from "../alchemy.run.ts";
export type CloudflareEnv = typeof worker.Env;
declare global {
type Env = CloudflareEnv;
}
declare module "cloudflare:workers" {
namespace Cloudflare {
export interface Env extends CloudflareEnv {}
}
}

The CLI updated the tsconfig.json to include alchemy.run.ts and register @cloudflare/workers-types + types/env.d.ts globally

{
"exclude": ["test"],
"include": ["types/**/*.ts", "src/**/*.ts", "alchemy.run.ts"],
"compilerOptions": {
"target": "es2021",
"lib": ["es2021"],
"jsx": "react-jsx",
"module": "es2022",
"moduleResolution": "Bundler",
// ... (omitted for brevity)
"types": ["@cloudflare/workers-types", "./types/env.d.ts"]
}
}