Introduction
What is this?
The ArkEnv plugin for Vite lets you validate environment variables at build-time with ArkEnv.
import from "@arkenv/vite-plugin";
import { } from "vite";
export default ({
: [
({
: "number.port",
: "string",
}),
],
});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")Validator Mode
By default, ArkEnv uses ArkType for validation. However, you can use any Standard Schema compatible library (like Zod or Valibot) by setting the validator option to "standard".
In this mode, you don't need to install arktype.
import { defineConfig } from "vite";
import arkenv from "@arkenv/vite-plugin";
import { z } from "zod";
export default defineConfig({
plugins: [
arkenv({
VITE_API_URL: z.url(),
VITE_API_KEY: z.string().min(1),
}, {
validator: "standard"
}),
],
});Important
By default, this plugin uses ArkType for validation.
You can also use validator: "standard" if you prefer using Zod or Valibot directly without an ArkType dependency.
See Validator mode for details.
Installation
npm install @arkenv/vite-pluginpnpm add @arkenv/vite-pluginyarn add @arkenv/vite-pluginbun add @arkenv/vite-pluginIf you intend to use the default validator mode, you should also install arktype:
npm install arktypepnpm add arktypeyarn add arktypebun add arktypeFor some workflows (including using ArkEnv in Vite config and typing import.meta.env), a dedicated arkenv installation is needed. See ArkEnv quickstart for more instructions.