Introduction
What is this?
The ArkEnv plugin for Bun lets you validate environment variables at build-time and runtime with ArkEnv.
It supports a simple setup for most projects, automatically discovering your schema from ./src/env.ts or ./env.ts.
Usage
Simple setup (recommended)
The simple setup automatically discovers your schema from src/env.ts or env.ts. This requires configuration in both bunfig.toml (for Bun.serve) and your build script (for Bun.build).
1. Create your schema
import { } from "arkenv";
export default ({
: "string",
: "boolean",
});2. Configure for Bun.serve (dev mode)
[serve.static]
plugins = ["@arkenv/bun-plugin"]3. Configure for Bun.build (build mode)
import arkenv from "@arkenv/bun-plugin";
await Bun.build({
entrypoints: ["./app.tsx"],
outdir: "./dist",
plugins: [arkenv], // Auto-discovers src/env.ts
});Advanced setup
If you need to customize the schema location or prefer explicit configuration:
For Bun.build
Pass your schema directly to the plugin function:
import arkenv from "@arkenv/bun-plugin";
import { type } from "arkenv";
const Env = type({
BUN_PUBLIC_API_URL: "string",
BUN_PUBLIC_DEBUG: "boolean",
});
await Bun.build({
entrypoints: ["./app.tsx"],
outdir: "./dist",
plugins: [arkenv(Env)],
});For Bun.serve
Create a custom plugin file and reference it in bunfig.toml:
import arkenv from "@arkenv/bun-plugin";
import { type } from "arkenv";
const Env = type({
BUN_PUBLIC_API_URL: "string",
BUN_PUBLIC_DEBUG: "boolean",
});
export default arkenv(Env);[serve.static]
plugins = ["./plugins/arkenv.ts"]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 arkenv from "@arkenv/bun-plugin";
import { type } from "arkenv";
import { z } from "zod";
await Bun.build({
entrypoints: ["./app.tsx"],
outdir: "./dist",
plugins: [
arkenv(type({
BUN_PUBLIC_API_URL: z.url(),
BUN_PUBLIC_DEBUG: "boolean",
}))
],
});Important
This plugin requires ArkType to be installed.
For a zero-ArkType setup, use arkenv/standard directly in your app code instead.
See Standard Schema validators for details.
Features
- Static Analysis: Automatically replaces
process.env.VARIABLEwith validated values during the build. - Typesafety: Ensures your code only accesses variables defined in your schema.
- Secure Defaults: Only exposes variables prefixed with
BUN_PUBLIC_to the client bundle.NODE_ENVis also exposed when defined in your schema. You can customize this prefix by setting theenvoption in yourBun.build()configuration (e.g.,env: "MY_PUBLIC_*"to useMY_PUBLIC_instead). See Bun's bundler documentation for more details.
Installation
npm install @arkenv/bun-plugin arkenvpnpm add @arkenv/bun-plugin arkenvyarn add @arkenv/bun-plugin arkenvbun add @arkenv/bun-plugin arkenvIf you intend to use the default validator, you should also install arktype:
npm install arktypepnpm add arktypeyarn add arktypebun add arktype