To create a standalone Cloudflare Worker using the Vite plugin, follow these steps:
- Initialize a
package.json with "type": "module" to enable ES modules. - Install the required dependencies:
vite, @cloudflare/vite-plugin, and wrangler. - Create a
vite.config.ts file and include the cloudflare() plugin. - Create a Worker configuration file (
wrangler.jsonc or wrangler.toml) specifying the Worker's name and the entry point (main). - Create the Worker entry file (e.g.,
src/index.ts) exporting a default object with a fetch handler.
Once configured, you can use the following scripts:
dev: Starts the Vite development server.build: Builds the application.preview: Builds and then previews the application.deploy: Builds and deploys the Worker to Cloudflare.
# Install dependencies
npm i -D vite @cloudflare/vite-plugin wrangler
// package.json
{
"name": "cloudflare-vite-get-started",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "npm run build && vite preview",
"deploy": "npm run build && wrangler deploy"
}
}
// vite.config.ts
import { defineConfig } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";
export default defineConfig({
plugins: [cloudflare()],
});
// wrangler.jsonc
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "cloudflare-vite-get-started",
"compatibility_date": "2026-09-29",
"main": "./src/index.ts"
}
// src/index.ts
export default {
fetch() {
return new Response(`Running in ${navigator.userAgent}!`);
},
};