> ## Documentation Index
> Fetch the complete documentation index at: https://wemstudios.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Installing Blaze in Your Cloudflare Workers Project

> Install Blaze with npm, yarn, or pnpm, configure TypeScript, and import the middleware subpaths you need. Zero runtime dependencies included.

Blaze ships as a single npm package with zero runtime dependencies and full TypeScript types included — no `@types/blaze` package needed. The core framework (`createApp`, `Router`, `BlazeRequest`, `BlazeResponse`) lives under the `blaze` import. Each of the 12 built-in middleware modules lives under its own subpath export (for example `blaze/middleware/cors`) so your bundler only includes the middleware you actually import — unused modules are tree-shaken away automatically.

## Requirements

Before you install, make sure your environment meets these requirements:

* **Node.js 18 or later** — required for Wrangler and the local dev server.
* **Wrangler 3 or later** — the Cloudflare Workers CLI. Install with `npm install -g wrangler`.
* **TypeScript 5 or later** — optional but strongly recommended. Blaze is designed TypeScript-first and the `Env` generic only works with TypeScript.

## Install

Add Blaze to your project with your preferred package manager.

<CodeGroup>
  ```bash npm theme={null}
  npm install blaze
  ```

  ```bash yarn theme={null}
  yarn add blaze
  ```

  ```bash pnpm theme={null}
  pnpm add blaze
  ```
</CodeGroup>

## TypeScript setup

Configure your `tsconfig.json` to target the Web Standards environment that Cloudflare Workers run in. The three fields below are the ones that matter most for a Blaze project.

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ES2022", "WebWorker"],
    "moduleResolution": "bundler"
  }
}
```

* **`target: "ES2022"`** — Workers run on V8 with modern ES support. Targeting ES2022 avoids unnecessary downlevelling of async/await, top-level await, and class fields.
* **`lib: ["ES2022", "WebWorker"]`** — the `WebWorker` lib replaces `DOM`. It gives you the Web Platform globals (`Request`, `Response`, `Headers`, `ReadableStream`, `crypto`, etc.) that Workers expose without pulling in browser-only DOM types that don't exist in the Workers runtime.
* **`moduleResolution: "bundler"`** — this tells TypeScript to resolve subpath exports (like `blaze/middleware/cors`) correctly, which is required for Blaze's tree-shakeable middleware imports.

## Package exports

Blaze uses Node.js subpath exports to keep your bundle lean. Import only the middleware modules you use — your bundler (esbuild, Wrangler's built-in bundler, Vite) will exclude everything else.

| Import path                       | Contents                                                                   |
| --------------------------------- | -------------------------------------------------------------------------- |
| `blaze`                           | Core: `createApp`, `Router`, `BlazeRequest`, `BlazeResponse`, `BlazeError` |
| `blaze/middleware/cors`           | CORS headers and preflight handling                                        |
| `blaze/middleware/logger`         | Request/response logger with CF colo                                       |
| `blaze/middleware/bearer-auth`    | Bearer token validation                                                    |
| `blaze/middleware/basic-auth`     | HTTP Basic Auth                                                            |
| `blaze/middleware/jwt`            | JWT validation via Web Crypto (HS256, RS256)                               |
| `blaze/middleware/rate-limit`     | KV-backed sliding window rate limiter                                      |
| `blaze/middleware/cache`          | Cloudflare Cache API integration                                           |
| `blaze/middleware/compress`       | Brotli/gzip compression via Streams                                        |
| `blaze/middleware/request-id`     | UUID request ID on `req.id` and `X-Request-Id` header                      |
| `blaze/middleware/etag`           | ETag generation for cacheable responses                                    |
| `blaze/middleware/timeout`        | Request timeout with `next(err)` on expiry                                 |
| `blaze/middleware/secure-headers` | CSP, HSTS, X-Frame-Options, Referrer-Policy                                |

For example, to use CORS and the logger together:

```typescript theme={null}
import { createApp } from 'blaze'
import { cors } from 'blaze/middleware/cors'
import { logger } from 'blaze/middleware/logger'

const app = createApp<Env>()

app.use(logger())
app.use(cors({ origins: '*' }))
```

## Cloudflare Workers types

To get TypeScript types for Cloudflare-specific globals (`KVNamespace`, `D1Database`, `R2Bucket`, `DurableObjectNamespace`, `Queue`, `Ai`, etc.), install the official Cloudflare Workers types package as a dev dependency.

```bash theme={null}
npm install -D @cloudflare/workers-types
```

Then reference it in your `tsconfig.json` so TypeScript picks up the global type definitions:

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ES2022", "WebWorker"],
    "moduleResolution": "bundler",
    "types": ["@cloudflare/workers-types"]
  }
}
```

With this in place, properties like `req.env.KV` (typed as `KVNamespace`) and `req.env.DB` (typed as `D1Database`) will give you full autocompletion and compile-time checks.

<Tip>
  Export your Worker using Module Worker syntax — `export default { fetch: app.fetch }` — rather than the legacy `addEventListener("fetch", ...)` Service Worker pattern. Module Workers have lower cold-start latency, native access to `env` bindings as a function argument, and support for additional handlers like `scheduled` and `queue`. Wrangler scaffolds Module Workers by default when you run `npm create cloudflare@latest`.
</Tip>
