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

@openenvx/envtyped

v0.4.3

Published

A typed environment variable parser using Zod with support for defaults, transforms, and validation

Readme

@openenvx/envtyped

A typed environment variable parser using Zod with support for defaults, transforms, and validation.

Installation

bun add @openenvx/envtyped zod
# or
npm install @openenvx/envtyped zod
# or
yarn add @openenvx/envtyped zod

Usage

Basic Usage

import { createEnv, z } from "@openenvx/envtyped";

const env = createEnv({
  PORT: z.string().default("3000"),
  NODE_ENV: z.enum(["development", "production", "test"]),
  DATABASE_URL: z.string().url(),
});

// env is fully typed
env.PORT; // string
env.NODE_ENV; // 'development' | 'production' | 'test'
env.DATABASE_URL; // string

With Custom Environment

const env = createEnv(
  {
    API_KEY: z.string().min(1),
    DEBUG: z.boolean().default(false),
  },
  {
    API_KEY: "secret-key",
    DEBUG: "true",
  },
);

Transforms

const env = createEnv({
  PORT: z.string().transform((val) => Number(val)),
  FEATURE_FLAGS: z.string().transform((val) => val.split(",")),
});

// env.PORT is number
// env.FEATURE_FLAGS is string[]

Refinements

const env = createEnv({
  PORT: z
    .string()
    .transform((val) => Number(val))
    .refine((val) => val > 0 && val < 65536, {
      message: "PORT must be between 1 and 65535",
    }),
});

Helper Functions

booleanString()

Parses string 'true' or 'false' to actual boolean:

import { booleanString } from "@openenvx/envtyped";

const env = createEnv({
  DEBUG: booleanString().default("false"),
  ENABLE_CACHE: booleanString(),
});

// env.DEBUG is boolean (defaults to false)
// env.ENABLE_CACHE is boolean

numberString()

Parses string numbers to actual numbers:

import { numberString } from "@openenvx/envtyped";

const env = createEnv({
  PORT: numberString().default("3000"),
  TIMEOUT: numberString(),
});

// env.PORT is number (defaults to 3000)
// env.TIMEOUT is number

Error Handling

When validation fails, an EnvValidationError is thrown with detailed information:

import { createEnv, EnvValidationError, z } from "@openenvx/envtyped";

try {
  const env = createEnv({
    PORT: z.number(),
    DATABASE_URL: z.string().url(),
  });
} catch (error) {
  if (error instanceof EnvValidationError) {
    console.error("Validation failed:", error.message);
    console.error("Issues:", error.issues);
    // [
    //   { path: 'PORT', message: 'Expected number, received nan' },
    //   { path: 'DATABASE_URL', message: 'Invalid url' }
    // ]
  }
}

TypeScript

@openenvx/envtyped provides full type inference:

import { createEnv, z } from "@openenvx/envtyped";

const env = createEnv({
  PORT: z.number().default(3000),
  NODE_ENV: z.enum(["development", "production"]),
});

// TypeScript knows the exact types
const port: number = env.PORT;
const nodeEnv: "development" | "production" = env.NODE_ENV;

API

createEnv(schema, env?)

Creates a validated environment object.

  • schema: Record of Zod schemas keyed by environment variable name
  • env: Optional environment object (defaults to process.env)
  • Returns: Typed object with parsed values
  • Throws: EnvValidationError if validation fails

createEnvWithOptions(options)

Creates a validated environment object with options.

createEnvWithOptions({
  schema: {
    /* zod schemas */
  },
  env: {
    /* environment variables */
  },
  onValidationError: (issues) => {
    /* custom error handler */
  },
});

EnvValidationError

Error class thrown when validation fails.

Properties:

  • message: Human-readable error message
  • issues: Array of { path: string; message: string } objects

License

MIT