Introduction
The ArkEnv plugin for Vite lets you validate environment variables at build-time with ArkType.
Important
This plugin requires ArkType to be installed.
Features
- Static Analysis: Validates environment variables against your schema and configures Vite's
definebehavior to expose them viaimport.meta.env.VARIABLE, leaving the actual inlining to Vite. - Typesafety: Ensures your code only accesses variables defined in your schema.
- Secure Defaults: Only exposes variables prefixed with
VITE_to the client bundle.NODE_ENVis also exposed when defined in your schema.
Quickstart
1. Installation
npm install @arkenv/vite-plugin arkenv arktypepnpm add @arkenv/vite-plugin arkenv arktypeyarn add @arkenv/vite-plugin arkenv arktypebun add @arkenv/vite-plugin arkenv arktype2. Configure Plugin
Define your schema as an exported constant in vite.config.ts. This allows the schema to be imported for typesafety in other files.
import from "@arkenv/vite-plugin";
import { } from "arkenv";
import { } from "vite";
export const = ({
: "number.port",
: "string"
});
export default ({
: [()]
});3. Add Typesafety
To make import.meta.env fully typesafe, add the following to src/vite-env.d.ts:
/// <reference types="vite/client" />
type ImportMetaEnvAugmented = import("@arkenv/vite-plugin").ImportMetaEnvAugmented<
typeof import("../vite.config").Env
>;
interface ImportMetaEnv extends ImportMetaEnvAugmented {}
interface ImportMeta {
readonly env: ImportMetaEnv;
}Tip
For more details on advanced typing options, see Typing import.meta.env.
4. Use in your code
You can now use import.meta.env with full typesafety and autocompletion throughout your application.
const apiUrl = import.meta..;With this setup, if any environment variable is missing or invalid, your dev server won't start and your production build will fail with an error:
ArkEnvError: Errors found while validating environment variables
VITE_MY_VAR must be a string (was missing)
PORT must be an integer between 0 and 65535 (was "hello")Using Standard Schema validators (Zod, Valibot)
This plugin uses ArkType for validation, which natively supports Standard Schema. You can freely mix ArkType DSL strings with Zod or Valibot validators in the same schema - no extra configuration needed.
import from "@arkenv/vite-plugin";
import { } from "arkenv";
import { } from "vite";
import { } from "zod";
export const = ({
: .().(),
: .().(1),
: "boolean"
});
export default ({
: [()]
});const apiUrl = import.meta..;For advanced workflows, see using ArkEnv in Vite config.