Typing process.env
For most use cases, you'll want to access your environment variables in your client code. In a Bun project, this is done via process.env.
With ArkEnv, this can be typesafe with the one-time setup below. After this, each time you add/remove environment variables from your schema, your process.env will always be typesafe - no codegen needed.
Link to section: setupSetup
Important
You must have the core arkenv package installed as a dependency in your project. See ArkEnv quickstart for instructions.
Add this to a bun-env.d.ts file in your project root:
/// <reference types="bun-types" />
type ProcessEnvAugmented = import("@arkenv/bun-plugin").ProcessEnvAugmented<
typeof import("./src/env").default
>;
declare namespace NodeJS {
interface ProcessEnv extends ProcessEnvAugmented {}
}Note
Notice the line that references "./src/env" - if your env.ts file is somewhere else (say, "./env"), update it accordingly.
Link to section: usageUsage
Once set up, process.env is fully typesafe for your BUN_PUBLIC_* variables:
// TypeScript knows about your BUN_PUBLIC_* variables
const apiUrl = process.env.BUN_PUBLIC_API_URL; // ✅ Typesafe
const debug = process.env.BUN_PUBLIC_DEBUG; // ✅ TypesafeLink to section: how-it-worksHow it works
The ProcessEnvAugmented type:
- Extracts the inferred type from your schema (the result of
type()from arkenv) - Filters to only include variables matching the Bun prefix (defaults to
"BUN_PUBLIC_") - Makes them available on
process.envwith full type safety
Server-only variables (like PORT) are automatically excluded from the client bundle and won't appear in the augmented process.env type.
Important
Imports will break type augmentation
If the ProcessEnv augmentation does not work, make sure you do not have any import statements in bun-env.d.ts. See the TypeScript documentation for more information.