---
title: "Vite Plugin"
description: "Learn how to use the Sentry Cloudflare Vite plugin to instrument bundled dependencies at build time."
url: https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin/
---

# Vite Plugin | Sentry for Cloudflare

Available since: `v10.68.0`

The Sentry Cloudflare Vite plugin has **experimental** stability. Configuration options and behavior may change or be removed in any release.

The Sentry Cloudflare Vite plugin (`sentryCloudflareVitePlugin`) instruments your Worker at build time. It can:

1. **Instrument bundled dependencies** — automatically instruments supported packages in your bundle (such as database clients like `mysql`) at build time, giving you more traces out of the box.
2. **Auto-instrument your Worker entry** — optionally wraps your default export with `Sentry.withSentry()` and Durable Objects with `instrumentDurableObjectWithSentry()` at build time, so you don't need to modify your code.

**We recommend building your Cloudflare Worker with Vite and the `sentryCloudflareVitePlugin` plugin.** It's the most complete way to get tracing for bundled dependencies in the Workers runtime. If you already deploy with `wrangler` directly, see [Migrating From Wrangler](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#migrating-from-wrangler).

## [Install](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#install)

The Vite plugin ships with `@sentry/cloudflare`, so there's no extra package to install. It's designed to run alongside the [Cloudflare Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/).

## [Prerequisites](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#prerequisites)

The plugin relies on Node.js APIs (`diagnostics_channel`) at runtime, so your Worker must have the `nodejs_compat` compatibility flag enabled. See [Node.js Compatibility Entrypoint](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/nodejs-compat.md) for setup.

## [Configure](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#configure)

Enable `useDiagnosticsChannelInjection` to trace supported bundled dependencies, and wrap your handler with `withSentry` as usual:

```typescript
import { cloudflare } from "@cloudflare/vite-plugin";
import { sentryCloudflareVitePlugin } from "@sentry/cloudflare/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    cloudflare(),
    sentryCloudflareVitePlugin({
      _experimental: {
        useDiagnosticsChannelInjection: true,
      },
    }),
  ],
});
```

*Other available variations of the above snippet: typescript*

### [Auto-instrumentation (Experimental)](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#auto-instrumentation-experimental)

Alternatively, the plugin can wrap your Worker for you at build time, so you don't need `withSentry` in your code. Enable `autoInstrumentation` and the plugin reads your `wrangler.(jsonc|toml)` to find the entry point, Durable Objects, and workflows:

```typescript
import { cloudflare } from "@cloudflare/vite-plugin";
import { sentryCloudflareVitePlugin } from "@sentry/cloudflare/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    cloudflare(),
    sentryCloudflareVitePlugin({
      _experimental: {
        autoInstrumentation: true,
        useDiagnosticsChannelInjection: true,
      },
    }),
  ],
});
```

With auto-instrumentation, you can optionally provide Sentry options via a co-located `instrument.server.*` file (`.ts`, `.mts`, `.js`, `.mjs`, or `.cjs`) next to your Worker entry. Use `defineCloudflareOptions` for full type-checking:

```typescript
import { defineCloudflareOptions } from "@sentry/cloudflare";

export default defineCloudflareOptions((env) => ({
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
}));
```

If no `instrument.server.*` file exists, the SDK reads all configuration (DSN, release, environment, sample rate, etc.) from the Worker's `env` bindings at runtime.

## [Options](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#options)

### [\_experimental](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#_experimental)

| Type | `object` |
| ---- | -------- |

Experimental options that may change or be removed without notice.

### [\_experimental.autoInstrumentation](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#_experimental.autoInstrumentation)

| Type    | `boolean` |
| ------- | --------- |
| Default | `false`   |

Automatically wraps your Worker at build time so you don't have to edit your entry. The plugin reads your wrangler config, wraps the default export with `Sentry.withSentry()` (sourcing options from a co-located `instrument.server.*` file, falling back to `env`), and wraps any configured Durable Object and Workflow classes with the matching `instrument*WithSentry` helper. Both `vite build` and `vite dev` are instrumented.

### [\_experimental.useDiagnosticsChannelInjection](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#_experimental.useDiagnosticsChannelInjection)

| Type    | `boolean` |
| ------- | --------- |
| Default | `false`   |

Enables build-time automatic instrumentation of supported dependencies. When enabled, the plugin injects `diagnostics_channel` calls into bundled packages during both `vite build` and `vite dev`. When disabled or omitted, the plugin is a no-op.

## [Migrating From Wrangler](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/vite-plugin.md#migrating-from-wrangler)

If you deploy with `wrangler` directly, moving to Vite is straightforward:

1. Set up the [Cloudflare Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/get-started/) and add a `vite.config.ts` with the `cloudflare()` and `sentryCloudflareVitePlugin()` plugins as shown above.
2. Run `vite build` before `wrangler deploy`, and use `vite dev` in place of `wrangler dev` for local development.

Your existing `wrangler.jsonc` becomes the input config — the plugin generates the deployed output during the build. For the full list of fields that change or become redundant, see Cloudflare's [Migrating from Wrangler](https://developers.cloudflare.com/workers/vite-plugin/reference/migrating-from-wrangler-dev/) guide.
