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

@ameykhoje/env-guard

v1.1.2

Published

Type-safe environment validation library

Readme

@ameykhoje/env-guard

Type-safe environment variable validation for JavaScript and TypeScript.

Validate, transform and safely access environment variables with support for:

  • String validation
  • Number validation
  • Enum validation
  • Boolean parsing
  • Arrays
  • Optional values
  • Defaults
  • Custom validators
  • Partial success with collected errors
  • End-to-end TypeScript inference

Installation

npm install @ameykhoje/env-guard

or

yarn add @ameykhoje/env-guard

Quick Start

import {
  env,
  string,
  number,
} from "@ameykhoje/env-guard";

const config = env(
  {
    VITE_API_URL:
      string()
        .url()
        .compile(),

    VITE_PORT:
      number()
        .min(1000)
        .max(9999)
        .compile(),
  },

  import.meta.env,
);

console.log(config.success);

console.log(config.data);

Result:

{
  success: true,

  data: {
    VITE_API_URL:
      "https://api.com",

    VITE_PORT:
      3000
  },

  errors: []
}

Parsing Result

env() never crashes automatically.

const result =
  env(schema);

if (!result.success) {
  console.log(
    result.errors,
  );
}

console.log(
  result.data,
);

Force strict mode:

const config =
  result.unwrap();

Throws when validation fails.


String Builder

string()
  .minLength(4)
  .regex(/abc/)
  .email()
  .url()
  .uuid()
  .optional()
  .default("value")
  .compile()

Example:

USERNAME:

string()
  .minLength(3)
  .compile()

Number Builder

number()
  .min(1)
  .max(100)
  .optional()
  .default(10)
  .compile()

Example

PORT:

number()
  .min(1000)
  .max(9999)
  .compile()

Boolean Builder

boolean()
  .default(false)
  .optional()
  .compile()

Supports:

DEBUG=true
DEBUG=false
DEBUG=1
DEBUG=0

Array Builder

array(
  number()
    .min(5)
    .compile(),
)

.compile()

Environment:

PORTS=10,20,30

Result:

[10,20,30]

Nested validation supported:

array(
  number()
    .min(5)
    .max(100)
    .compile(),
)

Optional Values

TOKEN:

string()
  .optional()
  .compile()

Missing value:

TOKEN === undefined

Defaults

HOST:

string()
  .default(
    "localhost",
  )

  .compile()

Missing value:

HOST === "localhost"

Custom Validation

string()

.custom(
  value =>
    value.startsWith(
      "sk_",
    ),

  "Invalid API key",
)

.compile()

Email Validation

EMAIL:

string()
  .email()
  .compile()

UUID Validation

ID:

string()
  .uuid()
  .compile()

Result Shape

Success:

{
  success: true,

  data: { ... },

  errors: []
}

Failure:

{
  success: false,

  data: {
    partial:
      "values"
  },

  errors: [
    {
      key:
        "PORT",

      message:
        "Minimum acceptable only 1000",

      received:
        500
    }
  ]
}

Example With Vite

.env

VITE_API_URL=https://api.com
VITE_PORT=3000

Usage:

const config =
  env(
    {
      VITE_API_URL:
        string()
          .url()
          .compile(),

      VITE_PORT:
        number()
          .compile(),
    },

    import.meta.env,
  );

Example With Node.js

const config =
  env(
    schema,
    process.env,
  );

Upcoming Features

  • Number Validation - Posivite, Negative, Finite
  • Array - Unique Elements, Non Empty, Length
  • Date(new)
  • Transformation (new)
  • Built-in dotenv loading
  • Multiple env support
  • Prefix validation. Eg. VITE_, NEXT_PUBLIC_
  • Asynchronous validations
  • Generate .env.example

License

MIT