npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

guardian-env

v1.0.6

Published

A lightweight TypeScript-first environment variable validator for Node.js projects

Readme

guardian-env

Catch missing or invalid environment variables before your app starts — with zero config.

npm version license node

npm install guardian-env

The Problem

Every Node.js app reads process.env, but env vars are untyped strings — a missing DATABASE_URL or a typo in PORT only blows up at runtime, deep inside your app, with a cryptic error.

guardian-env catches these issues at startup with a clear, actionable error. No runtime surprises.


Zero Config: Just Run It

Point it at your project and go:

npx guardian-env check

It scans your source code for every process.env.KEY and import.meta.env.KEY in use, then compares against your .env file. Missing keys fail immediately:

  guardian-env — auto mode
  Reference: source code (4 keys found)

  ── Missing Variables (2) ──────────────────────────────
  ✖ DATABASE_URL   → used in code but not set in .env
  ✖ JWT_SECRET     → used in code but not set in .env

  KEY              TYPE    VALUE
  ──────────────────────────────────────────────────────
  PORT             number  3000
  API_URL          url     http://localhost:3000/api
  ──────────────────────────────────────────────────────

  ✖ Validation failed (2 missing)
  Add the missing variables to your .env file.

All common access patterns are supported:

process.env.API_KEY                          // Node.js
import.meta.env.PUBLIC_API_URL               // Vite / SvelteKit
const { DB_HOST, DB_PORT } = process.env     // Destructuring

If no source code is found, guardian-env falls back to comparing against .env.example.


Validators

| Validator | Description | Example | |---|---|---| | string() | Any text value | string().min(8).max(100).matches(/regex/) | | number() | Integer or float | number().int().min(0).max(65535) · number().port() | | boolean() | Accepts true/false/1/0/yes/no/on/off | boolean().default(false) | | url() | Valid URL | url().protocols("postgresql", "https") | | email() | Valid email address | email().optional() | | enumValidator() | Restrict to allowed values | enumValidator(["a", "b"] as const) |

Modifiers — available on all validators:

string().optional()         // undefined if the variable is missing
string().default("value")   // fallback value if the variable is missing
string().describe("hint")   // shown in generated .env.example output

Config via package.json

No extra config file needed. Add a "guardian-env" key to your package.json:

{
  "guardian-env": {
    "ignore": ["SITE", "ORIGIN"],
    "src": "src"
  }
}

| Option | Description | |---|---| | ignore | Keys to skip during validation (e.g. framework-injected vars like SITE, ORIGIN) | | src | Directory to scan for env usage (default: project root) |


Advanced

Override validators for specific environments:

const env = defineEnv({
  LOG_LEVEL: string(),
}).forEnv({
  development: { LOG_LEVEL: string().default("debug") },
  production:  { LOG_LEVEL: string().default("error") },
});
db: group(
  { URL: url() },
  {
    prefix: "DB_",
    envSpecific: {
      test: { URL: url().default("postgresql://localhost/test") },
    },
  },
),

Get a list of errors instead of throwing:

const errors = env.validate(); // returns EnvError[] instead of throwing

Pass a mock env object directly when parsing:

const config = env.parse({
  env: { DB_URL: "postgresql://localhost/test" },
  nodeEnv: "test",
});

Write your own validator for special rules:

import { CustomValidator } from "guardian-env";

const hexColor = new CustomValidator<string>((raw) => {
  if (/^#[0-9a-fA-F]{6}$/.test(raw)) return { ok: true, value: raw };
  return { ok: false, error: `Expected a hex color, got "${raw}"` };
});

const env = defineEnv({ BRAND_COLOR: hexColor });

License

MIT © Doan Nguyen