@vite-env/core
The env.ts layer for Vite. You define your variables once, validate them everywhere, and import them with types.
view .mdopen in claudeopen in chatgpt
This plugin does for Vite what @t3-oss/env does for Next.js. It is built natively on Vite 8 and Rolldown. You define your environment variables once with a schema. You then get validated, typed imports everywhere.
Why I Built This
I wrote about four problems with plain Vite environment variables: no validation, no type safety, string-only values, and easy server-client leaks. See the four problems with plain Vite environment variables. This plugin solves all four problems.
Key Features
- Typed virtual modules: You import
envwith full IntelliSense. - Server/client split: If client code imports the server module, the build fails.
- Leak scan: If a server value appears in a client chunk, the build fails.
- Zod v4 native validation: The plugin also supports Standard Schema, including Valibot and ArkType.
- Auto
.d.tsgeneration: You get type safety without manual declarations. - Platform presets: The plugin supports Vercel, Railway, and Netlify.
- HMR
.envwatching: Changes reload without a restart. loadEnv(): Seed and migration scripts outside Vite use the sameenv.ts.- CLI:
vite-env checkvalidates in CI,generatewrites.env.example, andtypesregeneratesvite-env.d.ts.
Technical Details
The plugin generates virtual modules at build time. It splits server and client variables into separate imports. If client code imports a server variable by accident, the build fails. This stops secret leaks before they happen.
Getting started
- Install
@vite-env/coreand add the plugin to your Vite config withconfigFile: "./env.ts". - Create an
env.tsfile with your schema. - Import from
virtual:env/clientorvirtual:env/server. - Type-check your environment variables at build time.
The full guide is in the documentation.
Why it matters
Environment variable bugs are silent and dangerous. A missing key returns undefined. A leaked secret exposes your backend. This plugin makes environment variables type-safe and validated. It catches errors at build time instead of runtime.