anchor

TypeScript · Zero dependencies · 2.1 kB

Catch missing env vars before they ship.

Anchor validates your environment variables at build time and infers their types automatically — so a typo never becomes a 2 a.m. production page.

import { defineEnv } from "@anchor/env";

export const env = defineEnv({
  DATABASE_URL:  url(),
  PORT:          number().default(3000),
  STRIPE_KEY:    string().startsWith("sk_"),
});
// ✅ fully typed. validated at build time.
12.4kstars
340kweekly installs
68contributors
MITlicensed

Why Anchor

Everything you’d expect. Nothing you wouldn’t.

Build-time validation
Fails the build, not production.
Automatic type inference
Your env object is fully typed, no generics.
Framework-agnostic
Next, Vite, Node, Bun, anywhere.
Zero runtime cost
Validation compiles away.
Composable schemas
Share and extend across services.
Errors that help
Points at the exact missing/invalid key.

// how it works

From schema to safety in three steps.

Define your variables once. Anchor validates them at build time and hands you a fully typed object — no generics, no runtime cost.

env.ts
import { defineEnv, string, number, url } from "@anchor/env";

export const env = defineEnv({
  DATABASE_URL: url(),
  PORT:         number().default(3000),
  STRIPE_KEY:   string().startsWith("sk_"),
});

Quickstart

Install it. Define it. Import it.

01

Install

Add Anchor with your package manager of choice.

terminal
npm i @anchor/env
02

Define your schema

Describe your environment once, validated at build time.

env.ts
import { defineEnv, url } from "@anchor/env";

export const env = defineEnv({
  DATABASE_URL: url(),
});
03

Import anywhere

Get a fully typed, validated object — no setup required.

app.ts
import { env } from "./env";

env.DATABASE_URL; // fully typed