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 🙏

© 2025 – Pkg Stats / Ryan Hefner

safest-env

v0.0.1

Published

A tiny library for precise checking environment variables

Readme

safest-env

A tiny TypeScript library for precise validation of environment variables using Zod.

Lightweight, typed helper to validate process.env values at application startup and to convert/coerce common types (numbers, integers, booleans, URLs, emails, enums, etc.).

Repository: github.com/mirasayon/safest-env

NPM package: www.npmjs.com/package/safest-env

npm version

Why

  • Avoid runtime crashes due to missing or malformed environment variables.
  • Keep validation logic in a single place and get typed env values for the rest of your app.
  • Built on top of Zod so you can combine built-in validators with custom Zod schemas.

Install

npm install safest-env

Quick example

import { validateEnvironment, builtInSchemas as v } from "safest-env";
const env = validateEnvironment({
    NODE_ENV: v.enum(["development", "test", "production"] as const),
    PORT: v.integer(),
    DEBUG: v.stringbool().optional(),
});
// `env` is typed: env.PORT is number, env.NODE_ENV is 'development' | 'production', etc.
console.log("Port: ", env.PORT);

API

validateEnvironment(config)

Validate process.env against a record of Zod schemas.

  • Input: { [ENV_NAME]: ZodSchema }
  • Output: typed record { [ENV_NAME]: ParsedValue } or throws:
    • NoRequiredEnvVariableError — if a required env var is missing or empty.
    • InvalidEnvVariableTypeError — if a value fails validation; error contains cause and messages from Zod.

Notes:

  • This package is ESM-only and ships types
  • If a schema accepts undefined (e.g. .optional()), the variable is considered optional.
  • The function uses schema.safeParse(process.env[ENV_NAME]) so it will coerce when you pass coercion schemas such as z.coerce.number().

builtInSchemas

A small helper collection returning common Zod schemas:

  • .string()z.string()
  • .email()z.string().email()
  • .url()z.string().url()
  • .number()z.coerce.number()
  • .integer()z.coerce.number().int()
  • .stringbool(opts?) — string -> boolean coercion using common truthy/falsy values (configurable)
  • .enum([...])z.enum([...])

Errors

The library throws two custom error classes to help you catch and log problems early:

  • NoRequiredEnvVariableError — thrown when a non-optional env var is missing or the value is an empty string.
  • InvalidEnvVariableTypeError — thrown when a value fails schema validation. The error message contains Zod's treeifyError(...).errors list.

License

MIT