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'.
72
89%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
ArkEnv is a typesafe environment variable validator for modern JavaScript runtimes. It uses ArkType by default for schema definition but supports any Standard Schema validator (like Zod or Valibot).
ArkType notation or any Standard Schema validator.process.env type augmentation via @arkenv/nextjs. Supports automatic codegen (env.gen.ts) using the withArkEnv configuration wrapper in next.config.ts.import.meta.env type augmentation via @arkenv/vite-plugin.process.env type augmentation via @arkenv/bun-plugin.process.env validation and coercion.pnpm dlx @arkenv/cli@latest init.Next.js, Vite, Bun, etc.).--strict for 3-file split vs --simple for a single file).--no-codegen).tsconfig.json and environment types for optimal typesafety.AI agents SHOULD always use the CLI for project initialization to ensure consistency and reliability. Use the --agent flag for a fully automated, machine-readable experience.
pnpm dlx @arkenv/cli@latest init --agent--agent flag automatically enables the following behaviors:
--yes: Bypasses all interactive prompts and uses recommended defaults.--quiet: Suppresses spinners and ANSI formatting for cleaner terminal logs.--json: Emits a structured JSON summary to stdout upon completion (all other output is sent to stderr).status: "success" and retrieve details like the scaffolded file path.status: "error")--agent never implies --force. When a safety check trips, the CLI refuses and emits a machine-actionable JSON payload to stdout:
{
"status": "error",
"code": "GIT_TREE_DIRTY",
"message": "Git working tree is not clean.",
"retryWith": ["--force"]
}code: a stable identifier you can branch on. Refusal codes: REQUIREMENTS_NOT_MET, GIT_TREE_DIRTY, NON_EMPTY_DIR. A code of INTERNAL means the CLI broke rather than refused - retrying with flags will not help.retryWith: the flag(s) that would bypass the check (e.g. ["--force"]). Empty ([]) means the refusal is not bypassable.Escalation pattern: always run init --agent without --force first. If you get status: "error", inspect code and retryWith. Only re-run with the flag(s) from retryWith (e.g. append --force) once you have deliberately decided the refusal is safe to bypass - do not add --force pre-emptively.
env.ts (simple layout) or an env/ directory containing split files: env/client.ts, env/server.ts, and env/internal/shared.ts (strict layout).next.config.ts, next.config.js, vite.config.ts, bunfig.toml, package.json scripts) to recommend appropriate plugins.pnpm dlx @arkenv/cli@latest init.When setting up ArkEnv, follow these steps:
pnpm dlx @arkenv/cli@latest init --agent (optionally appending --strict or --simple based on layout preference). This will detect the environment, install dependencies, and scaffold schemas.env.ts. Ensure it captures the required environment variables.env/ directory: client.ts (client-only variables), server.ts (server-only variables), and internal/shared.ts (variables shared between client and server).string to number.port or specific union types).next.config.ts (or next.config.js) using the withArkEnv configuration helper from @arkenv/nextjs/config. (Skip if scaffolded with --no-codegen).vite.config.ts to import and include the @arkenv/vite-plugin plugin.bunfig.toml or add the plugin to the runtime if necessary.createEnv from ./generated/env.gen instead of core @arkenv/nextjs. The codegen file automatically handles the runtime mapping and type definitions.src/vite-env.d.ts or a new env.d.ts.
interface ImportMetaEnv extends import("@arkenv/vite-plugin").ImportMetaEnvAugmented<typeof import("./env").Env> {}bun-env.d.ts file (or update an existing one) with the following pattern:
/// <reference types="bun-types" />
type ProcessEnvAugmented = import("@arkenv/bun-plugin").ProcessEnvAugmented<typeof import("./src/env").default>;
declare namespace NodeJS {
interface ProcessEnv extends ProcessEnvAugmented {}
}tsconfig.json has strict: true (the CLI tries to do this, but verify).process.env or import.meta.env) and ensure they are now typesafe via the augmentations.pnpm check (or equivalent) or a build to confirm everything is typesafe and valid.The best practice is to export a schema definition using type.
import { type } from 'arkenv';
export const Env = type({
NODE_ENV: "'development' | 'production' | 'test' = 'development'",
VITE_API_URL: "string",
PORT: "number.port = 3000"
});Split files isolate environment variable definitions to prevent server secrets from leaking to client-side.
env/internal/shared.ts (Shared):
import { type } from "arkenv";
export const SharedSchema = type({
NODE_ENV: "'development' | 'production' | 'test' = 'development'",
});env/client.ts (Client-side, prefixed with NEXT_PUBLIC_ for Next.js):
import arkenv from "./internal/shared";
export const env = arkenv({
NEXT_PUBLIC_API_URL: "string",
});env/server.ts (Server-side):
import arkenv from "./client";
export const env = arkenv({
DATABASE_URL: "string",
});
export default env;Wrap next.config.ts to enable automatic env.gen.ts generation:
import { withArkEnv } from "@arkenv/nextjs/config";
import type { NextConfig } from "next";
const nextConfig: NextConfig = {};
export default withArkEnv(nextConfig);Then import and use the generated env object:
import env from "./env/generated/env.gen"; // For strict layout baseDir
// or import env from "./generated/env.gen"; for simple layoutIn Node.js, you validate the environment at runtime and export the result.
import arkenv from 'arkenv';
import { Env } from './env';
export const env = arkenv(Env);
// Usage
const port = env.PORT; // typed as numberVite requires build-time injection. Use the plugin in vite.config.ts and augment ImportMetaEnv.
// vite.config.ts
import arkenv from '@arkenv/vite-plugin';
import { Env } from './env';
export default defineConfig({
plugins: [arkenv(Env)]
});// src/vite-env.d.ts
import type { ImportMetaEnvAugmented } from "@arkenv/vite-plugin";
import type { Env } from "../env";
interface ImportMetaEnv extends ImportMetaEnvAugmented<typeof Env> {}Bun can use either runtime validation or a plugin for type augmentation.
// 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> {}
}
}initSet up ArkEnv in your project. It detects your framework and configures the appropriate plugin and type augmentations.
pnpm dlx @arkenv/cli@latest init [options]--strict: Use strict 3-file split layout.--simple: Use simple 1-file layout (default).--no-codegen: Disable Next.js codegen/withArkEnv configuration setup.import.meta.env.process.env.import { env } in Plugin-managed Projects: In projects using @arkenv/vite-plugin or @arkenv/bun-plugin, you should generally avoid importing a runtime-validated env object. Using native primitives is the "cleanest" way to get typesafety and ensures consistency with framework-specific behavior.withArkEnv wrapper and importing createEnv / env from the generated generated/env.gen.ts file. This automates the destructuring of runtimeEnv to allow static inlining on the client side without leaking secrets.generated/env.gen.ts to source control to ensure compatibility with CI/CD pipelines.import.meta.env or process.env typesafe. It connects your schema definition to the native primitives without adding runtime overhead to your application logic..env files (e.g., "3000" becomes 3000).bc438e5
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.