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

env-type-safe

v1.0.1

Published

A type-safe environment variable manager

Downloads

9

Readme

env-type-safe

A type-safe environment variable manager for Node.js applications with built-in validation and TypeScript support.

Features

  • 🔒 Type Safety: Full TypeScript support with type inference
  • Validation: Automatic type validation for strings, numbers, and booleans
  • 📁 Environment Files: Load variables from .env files (using dotenv)
  • 🛡️ Runtime Safety: Throws helpful errors for missing or invalid variables
  • 🔍 Debugging: Clear error messages for troubleshooting
  • 🎯 Simple API: Easy to use with minimal setup

Installation

npm install env-type-safe dotenv

Note: dotenv is a peer dependency and must be installed separately.

Quick Start

import { createEnvSafe } from 'env-type-safe';

// Define your environment schema
const env = createEnvSafe({
  PORT: "number",
  API_KEY: "string",
  DEBUG: "boolean",
}, { 
  envFile: ".env" // Optional: path to .env file
});

// Type-safe access to variables
const port: number = env.get<number>("PORT");       // Returns number
const apiKey: string = env.get<string>("API_KEY");  // Returns string
const debug: boolean = env.get<boolean>("DEBUG");    // Returns boolean

// Get all variables at once
const allVars = env.getAll();

Schema Types

The package supports three types of environment variables:

| Type | Description | Example | |-----------|------------------------------------------------|------------------------| | string | Any string value | API_KEY="xyz123" | | number | Numeric values (auto-converted from string) | PORT="3000" | | boolean | Boolean values (must be "true" or "false") | DEBUG="true" |

Error Handling

The package includes comprehensive error handling:

  1. Missing Variables

    // If DATABASE_URL is not defined:
    createEnvSafe({ DATABASE_URL: "string" })
    // Error: Environment variable DATABASE_URL is missing
  2. Invalid Types

    // If PORT="not-a-number" in .env:
    createEnvSafe({ PORT: "number" })
    // Error: Environment variable PORT must be a number
  3. Invalid Boolean Values

    // If DEBUG="yes" in .env:
    createEnvSafe({ DEBUG: "boolean" })
    // Error: Environment variable DEBUG must be 'true' or 'false'

Best Practices

  1. Define Schema Once

    // config/env.ts
    export const envSchema = {
      NODE_ENV: "string",
      PORT: "number",
      DEBUG: "boolean",
      API_KEY: "string"
    };
    
    export const env = createEnvSafe(envSchema);
  2. Use Type Inference

    // TypeScript will infer the correct types
    const { PORT, API_KEY, DEBUG } = env.getAll();
  3. Environment File

    PORT=3000
    DEBUG=true
    API_KEY=your-secret-key

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT

Author

MD Azadur Rahman