> ## Documentation Index
> Fetch the complete documentation index at: https://better-router.bansal.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment and secrets

> Where secrets live, getEnv vs process.env vs import.meta.env.

`getEnv()` is the request's `c.env`. On Cloudflare that is the Worker bindings object. On Node during `vite dev`, the Hono adapter exposes `.env` files on `process.env`, and the request context still has an `env` object.

```ts theme={null}
import { getEnv } from "better-router";

export function loader() {
  const env = getEnv();
  return { appUrl: env.APP_URL };
}
```

Prefer `getEnv()` in loaders, actions, and middleware so the same code runs on Workers. Do not sprinkle `process.env` through those files.

## Files

| File                    | Used by                      | Commit? |
| ----------------------- | ---------------------------- | ------- |
| `.env` / `.env.local`   | Vite, Node `vite dev`        | no      |
| `.dev.vars`             | `wrangler dev`               | no      |
| `wrangler.jsonc` `vars` | Cloudflare Workers (public)  | yes     |
| `wrangler secret put`   | Cloudflare Workers (private) | no      |

Do not put secrets in `vite.config.ts` or client pages. Anything referenced as `import.meta.env.VITE_*` is bundled for the browser.

## Node vs Workers

On Workers, `process.env` is not your Wrangler bindings. Bindings only exist on `c.env` / the `env` argument of `fetch` / `scheduled` / `queue` / `email`.

```ts title="src/worker.ts" theme={null}
import { defineWorker } from "better-router/worker";

export default defineWorker({
  async scheduled(_event, env) {
    const url = env.APP_URL;
    console.log("cron", url);
  },
});
```

Inside HTTP handlers, `getEnv()` is that same `env`. In `scheduled` / `queue` / `email`, pass `env` yourself — `getEnv()` is request-scoped and will throw.

## Client env

The browser only sees `import.meta.env.VITE_*`. Use that for public values such as `VITE_APP_URL`. Never prefix secrets with `VITE_`.

## `.env` example

```bash title=".env" theme={null}
APP_URL=http://localhost:5173
```
