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

@arthur-lobo/zod-env

v1.0.1

Published

load and validate your env with zod.

Readme

Zod Env

A TypeScript library for loading and validating environment variables using Zod, providing strong and safe type validation for environment configurations.

Installation

npm install @arthur-lobo/zod-env

Description

ZodEnv allows you to define Zod schemas to validate environment variables, ensuring their values are in the correct format and providing default values when necessary. It supports both synchronous and asynchronous operations, and allow you to define how .env must be loaded.

Basic Usage

Synchronous Class: ZodEnv

import { z } from 'zod';
import { ZodEnv } from '@arthur-lobo/zod-env';

const schema = z.object({
  PORT: z.coerce.number().default(3000),
  DATABASE_URL: z.string(),
  DEBUG: z.coerce.boolean().default(false),
});

const env = new ZodEnv({
  schema,
  getEnv: () => process.env, // or any function that returns an object with the variables like `import.meta.env` when using vite to build your frontend
});

// Access validated values
console.log(env.get('PORT')); // 3000 (or the value of process.env.PORT if defined)
console.log(env.get('DATABASE_URL')); // validated string

Asynchronous Class: AsyncZodEnv

import { z } from 'zod';
import { AsyncZodEnv } from '@arthur-lobo/zod-env';

const schema = z.object({
  API_KEY: z.string(),
  TIMEOUT: z.coerce.number().default(5000),
});

const env = new AsyncZodEnv({
  schema,
  getEnv: async () => {
    await someAsyncOperation();
    return process.env;
  },
});

// Access validated values asynchronously
const apiKey = await env.get('API_KEY');
const timeout = await env.get('TIMEOUT');

Loading .env File

You can automatically load a .env file using Node.js's loadEnv function:

import { loadEnvFile } from 'node:process';

const env = new ZodEnv({
  schema: z.object({
    SECRET_KEY: z.string(),
  }),
  loadEnv: loadEnvFile, // Loads the .env file
  getEnv: () => process.env,
});

console.log(env.get('SECRET_KEY')); // Validated value from .env

For asynchronous operations:

const env = new AsyncZodEnv({
  schema: z.object({
    SECRET_KEY: z.string(),
  }),
  loadEnv: async () => loadEnvFile(), // Loads .env asynchronously
  getEnv: async () => process.env,
});

Schema Examples

import { z } from 'zod';

// Basic schema with default values
const basicSchema = z.object({
  PORT: z.coerce.number().default(3000),
  HOST: z.string().default('localhost'),
  DEBUG: z.coerce.boolean().default(false),
});

// Schema with custom validations
const advancedSchema = z.object({
  DATABASE_URL: z.string().url(),
  JWT_SECRET: z.string().min(32, 'JWT secret must be at least 32 characters'),
  MAX_CONNECTIONS: z.coerce.number().min(1).max(100),
  ALLOWED_ORIGINS: z.string().transform(str => str.split(',')).pipe(z.array(z.string().url())),
});

Benefits

  • Strong Type Validation: Uses Zod to ensure environment variables are in the correct format.
  • Default Values: Native support for default values in the Zod schema.
  • Flexibility: Works with any source of environment variables (process.env, import.meta.env, custom files, etc.).
  • .env Support: Easy integration with .env files via Node.js's loadEnvFile.
  • Async/Sync: Choose between synchronous or asynchronous operations as needed.
  • TypeScript: Fully typed, providing autocomplete and type checking.