@vite-env/core
v0.5.2
Published
The env.ts layer for Vite — define once, validate everywhere, import with types
Maintainers
Readme
@vite-env/core
The env.ts layer for Vite — define once, validate everywhere, import with types.
- Typed virtual modules (
virtual:env/client,virtual:env/server) - Server/client split with build-time leak detection
- Runtime access protection — warns or errors when
virtual:env/serveris imported from a client environment - Auto-coercion via Zod v4 (
z.stringbool(),z.coerce.number()) - Standard Schema support — use Valibot, ArkType, or any compliant validator
- Platform presets — pre-built schemas for Vercel, Railway, and Netlify
- Auto
.d.tsgeneration - Vite 8 / Rolldown native
Install
pnpm add @vite-env/core zodUsage
1. Define your schema — env.ts
import { defineEnv } from '@vite-env/core'
import { z } from 'zod'
export default defineEnv({
server: {
DATABASE_URL: z.url(),
JWT_SECRET: z.string().min(32),
},
client: {
VITE_API_URL: z.url(),
VITE_DARK_MODE: z.stringbool().default(false),
VITE_NODE_ENV: z
.enum(['development', 'test', 'production'])
.default('development'),
},
})2. Add the plugin — vite.config.ts
import ViteEnv from '@vite-env/core/plugin'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [ViteEnv()],
})3. Import typed env
import { env } from 'virtual:env/client'
env.VITE_API_URL // string
env.VITE_DARK_MODE // boolean
env.VITE_NODE_ENV // 'development' | 'test' | 'production'Platform presets
import { defineEnv } from '@vite-env/core'
import { vercel } from '@vite-env/core/presets'
import { z } from 'zod'
export default defineEnv({
presets: [vercel],
server: { DATABASE_URL: z.url() },
client: { VITE_API_URL: z.url() },
})Available presets: vercel, railway, netlify.
See the full documentation for server/client split details, CLI tools, and more.
