arkenv
Original:🇺🇸 English
Translated
Answer questions about ArkEnv and help implement environment variable validation. Use when developers: (1) Ask about environment variable validation or typesafety, (2) Want to setup ArkEnv in a project, (3) Need to define or update schemas using ArkType or Standard Schema, (4) Are integrating with Vite, Bun, or other runtimes. Triggers on: 'ArkEnv', 'env validation', 'typesafe env', 'createEnv', 'env.ts', '@arkenv/cli'.
5installs
Sourceyamcodes/arkenv
Added on
NPX Install
npx skill4agent add yamcodes/arkenv arkenvTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →ArkEnv
ArkEnv is a typesafe environment variable validator for modern JavaScript runtimes. It uses by default for schema definition but supports any validator (like or ).
ArkTypeStandard SchemaZodValibotCapabilities
Core Usage
- Define typesafe schemas using notation or any
ArkTypevalidator.Standard Schema - Implement complex types, arrays, and unions.
- Configure automatic coercion and default values.
- Follow best practices for schema organization.
Framework Integration
- Vite: Build-time validation and type augmentation via
import.meta.env.@arkenv/vite-plugin - Bun: Build-time/Runtime validation and type augmentation via
process.env.@arkenv/bun-plugin - Node.js: Standard validation and coercion.
process.env
CLI (Setup & DevOps)
- Initialize ArkEnv in new or existing projects using .
pnpm dlx @arkenv/cli@latest init - Scaffold schema files and detect framework-specific configurations (,
Vite, etc.).Bun - Automatically configure and environment types for optimal typesafety.
tsconfig.json
Agent Setup (Machine-Readable)
AI agents SHOULD always use the CLI for project initialization to ensure consistency and reliability. Use the flag for a fully automated, machine-readable experience.
--agent- Command:
pnpm dlx @arkenv/cli@latest init --agent - Behavior: The flag automatically enables the following behaviors:
--agent- : Bypasses all interactive prompts and uses recommended defaults.
--yes - : Suppresses spinners and ANSI formatting for cleaner terminal logs.
--quiet - : Emits a structured JSON summary to
--jsonupon completion (all other output is sent tostdout).stderr
- Success Verification: Parse the JSON output to verify and retrieve details like the scaffolded file path.
status: "success"
Operational Logic
- Detection:
- Look for or
env.tsimports to understand existing schema.arkenv - Check for framework config files (,
vite.config.ts,bunfig.tomlscripts) to recommend appropriate plugins.package.json
- Look for
- Setup:
- If ArkEnv is not present or a fresh setup is requested, trigger the Setup Workflow.
- Prefer using the CLI for initialization: .
pnpm dlx @arkenv/cli@latest init - If the CLI cannot be used or fails, fall back to manual configuration.
Setup Workflow
When setting up ArkEnv, follow these steps:
- Initialize: Run . This will attempt to detect the environment, install dependencies, and scaffold a base
pnpm dlx @arkenv/cli@latest init --agent.env.ts - Review & Refine :
env.ts- Inspect the generated . Ensure it captures the required environment variables.
env.ts - Refine types (e.g., change to
stringor specific union types).number.port
- Inspect the generated
- Manual Plugin Configuration:
- The CLI installs plugins but might not update config files.
- Vite: Update to import and include the
vite.config.tsplugin.arkenv - Bun: Configure or add the plugin to the runtime if necessary.
bunfig.toml
- Typesafety & Augmentation:
- Vite: Add type augmentation to or a new
src/vite-env.d.ts.env.d.tstsinterface ImportMetaEnv extends import("@arkenv/vite-plugin").ImportMetaEnvAugmented<typeof import("./env").Env> {} - Bun: Create a file (or update an existing one) with the following pattern:
bun-env.d.tsts/// <reference types="bun-types" /> type ProcessEnvAugmented = import("@arkenv/bun-plugin").ProcessEnvAugmented<typeof import("./src/env").default>; declare namespace NodeJS { interface ProcessEnv extends ProcessEnvAugmented {} } - Ensure has
tsconfig.json(the CLI tries to do this, but verify).strict: true
- Vite: Add type augmentation to
- Usage Update: Scan the codebase for existing environment variable usage (or
process.env) and ensure they are now typesafe via the augmentations.import.meta.env - Validation: Run (or equivalent) or a build to confirm everything is typesafe and valid.
pnpm check
Core Concepts
Defining a Schema
The best practice is to export a schema definition using .
typets
import { type } from 'arkenv';
export const Env = type({
NODE_ENV: "'development' | 'production' | 'test' = 'development'",
VITE_API_URL: "string",
PORT: "number.port = 3000"
});Usage: Node.js (Standard)
In Node.js, you validate the environment at runtime and export the result.
ts
import arkenv from 'arkenv';
import { Env } from './env';
export const env = arkenv(Env);
// Usage
const port = env.PORT; // typed as numberUsage: Vite (Frontend)
Vite requires build-time injection. Use the plugin in and augment .
vite.config.tsImportMetaEnvts
// vite.config.ts
import arkenv from '@arkenv/vite-plugin';
import { Env } from './env';
export default defineConfig({
plugins: [arkenv(Env)]
});ts
// src/vite-env.d.ts
import type { ImportMetaEnvAugmented } from "@arkenv/vite-plugin";
import type { Env } from "../env";
interface ImportMetaEnv extends ImportMetaEnvAugmented<typeof Env> {}Usage: Bun
Bun can use either runtime validation or a plugin for type augmentation.
ts
// src/env.d.ts
import type { ProcessEnvAugmented } from "@arkenv/bun-plugin";
import type { Env } from "./env";
declare global {
namespace NodeJS {
interface ProcessEnv extends ProcessEnvAugmented<typeof Env> {}
}
}CLI Commands
init
initSet up ArkEnv in your project. It detects your framework and configures the appropriate plugin and type augmentations.
bash
pnpm dlx @arkenv/cli@latest initBest Practices
- Prefer Native Primitives: To leverage the full power of ArkEnv plugins, you should access environment variables through the runtime's native primitives.
- Vite: Use .
import.meta.env - Bun: Use .
process.env - This ensures that build-time validation, static replacement (Vite), and runtime optimizations (Bun) work as intended while remaining fully typesafe via type augmentation.
- Vite: Use
- Avoid in Plugin-managed Projects: In projects using
import { env }or@arkenv/vite-plugin, you should generally avoid importing a runtime-validated@arkenv/bun-pluginobject. Using native primitives is the "cleanest" way to get typesafety and ensures consistency with framework-specific behavior.env - Use Type Augmentation: This is the recommended way to make or
import.meta.envtypesafe. It connects your schema definition to the native primitives without adding runtime overhead to your application logic.process.env - Re-use Schema: Define your schema once and use it for both the plugin (build-time/config) and runtime validation if needed.
- Coercion: ArkEnv automatically coerces strings from files (e.g.,
.envbecomes"3000").3000