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

safe-ts-env

v1.0.0

Published

A TypeScript library that provides type-safe environment configuration.

Downloads

1

Readme

type-safe-env

A TypeScript library that provides type-safe environment configuration.

Installation

npm install type-safe-env

Features

  • Type Safety: Leverage TypeScript's type system to ensure your environment configurations are correctly typed
  • Runtime Validation: Use Zod schemas to validate environment configurations at runtime
  • Flexible Path Handling: Support for both string and array path formats
  • Error Handling: Built-in error handling with optional debug logging
  • Zero Configuration: Simple API that works out of the box

Usage

Basic Example

import { getEnv } from 'type-safe-env';
import { z } from 'zod';

// Define your environment schema using Zod
const envSchema = z.object({
  name: z.string(),
  region: z.string(),
  stage: z.enum(['dev', 'staging', 'prod']),
  instanceCount: z.number().int().positive(),
});

// Load and validate your environment configuration
const checkedEnv = getEnv('path/to/env/file.json', envSchema);

// TypeScript knows the exact shape of checkedEnv
console.log(checkedEnv.name); // string
console.log(checkedEnv.region); // string
console.log(checkedEnv.stage); // 'dev' | 'staging' | 'prod'
console.log(checkedEnv.instanceCount); // number

Using Array Path

You can also provide the path as an array of path segments:

const checkedEnv = getEnv(['path', 'to', 'env', 'file.json'], envSchema);

Using with process.env

const checkedEnv = getEnv('path/to/env/file.json', envSchema);

process.env = {
  ...process.env,
  ...Object.fromEntries(
    Object.entries(checkedEnv).map(([key, value]) => [key, String(value)]),
  ),
};

Using with CDK

// Define your environment schema using Zod
const envSchema = z.object({
  name: z.string(),
  region: z.string(),
  stage: z.enum(['dev', 'staging', 'prod']),
  instanceCount: z.number().int().positive(),
});
const checkedEnv = getEnv('path/to/env/file.json', envSchema);

const stackProps = {
  ...process.env,
  ...Object.fromEntries(
    Object.entries(checkedEnv).map(([key, value]) => [key, String(value)]),
  ),
};

// Use in your CDK stack
new MyStack(app, 'MyStack', stackProps);

Error Handling

The library will throw an error if:

  • The environment file cannot be found or read
  • The file content doesn't match the provided schema

You can enable debug logging by setting the DEBUG environment variable:

DEBUG=true npm run start

API Reference

getEnv<T>

Loads and validates environment configuration from a file.

Parameters

  • pathOfEnvFile: string | Array<string> - Path to the environment file
  • envSchema: z.ZodObject<any> - Zod schema for validating the environment configuration

Returns

  • z.infer<T> - The validated environment configuration with inferred types from the schema

Throws

  • Error - If the file cannot be found or read
  • z.ZodError - If the file content doesn't match the provided schema

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.