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

@jlerocher/ts-env

v1.0.2

Published

A lightweight, zero-dependency library to load, validate, and safely use environment variables in TypeScript — with full type inference and runtime validation.

Readme

🧩 @jlerocher/ts-env

npm version License: MIT Publish to npm

A lightweight, zero-dependency library to load, validate, and safely use environment variables in TypeScript — with full type inference and runtime validation.

🧠 Why

Working with process.env is unsafe — everything is a string, values can be missing, and bugs often appear too late in production. @jlerocher/ts-env ensures your environment configuration is predictable, validated, and fully typed from the start.


✨ Features

  • Automatic Environment Loading: Loads variables from .env and .env.[NODE_ENV] files automatically.
  • Type Inference: Automatically detects string, number, and boolean types from your .env files.
  • Type Generation: Generates a types.d.ts file, providing full TypeScript autocompletion and type checking for your environment variables.
  • Runtime Validation: Throws an error on startup if any environment variable is empty, ensuring no missing variables in production.
  • Zero Dependencies: Lightweight and easy to integrate into any project.

🧑‍💻 Installation

npm install @jlerocher/ts-env

🚀 Usage Example

  1. Create a .env file in the root of your project:

    # .env
    APP_NAME="My Awesome App"
    PORT=3000
    DEBUG_MODE=true
    API_KEY=abc-123-def-456
  2. Instantiate TsEnv in your project's entry point (e.g., index.ts):

    import createEnv from '@jlerocher/ts-env';
    
    // This will load the .env file, validate the variables,
    // and generate a `src/types.d.ts` file.
    const env = createEnv();
    
    export default env;
  3. Automatic Type Generation: The library will create a src/types.d.ts file for you:

    // src/types.d.ts (auto-generated)
    declare type Env = {
        readonly APP_NAME: string;
        readonly PORT: number;
        readonly DEBUG_MODE: boolean;
        readonly API_KEY: string;
    };
  4. Access Your Typed Variables: Now you can import and use your environment variables with full type safety and autocompletion.

    import env from './env-setup'; // Assuming you put the createEnv call in `env-setup.ts`
    
    console.log(env.get('APP_NAME')); // "My Awesome App" (string)
    console.log(env.get('PORT'));     // 3000 (number)
    console.log(env.get('DEBUG_MODE')); // true (boolean)
    
    // TypeScript will catch typos!
    // console.log(env.get('API_KEYY')); // Error: Argument of type '"API_KEYY"' is not assignable to parameter of type 'keyof Env'.

⚙️ API

createEnv(options?: TsEnvOptions): TsEnv

A factory function that creates and returns a TsEnv instance.

TsEnvOptions

You can pass an options object to createEnv to customize its behavior:

| Option | Description | Default | |-------------|-------------------------------------------------------------------------------------------------------------|----------------------------------------------| | dir | The directory to search for .env files. | process.cwd() | | env | The environment to load (e.g., 'production'). Loads .env.[env]. | process.env.NODE_ENV | | typesPath | Path for the generated .d.ts file. Set to null to disable. | path.join(process.cwd(), "src/types.d.ts") |


📄 License

This project is licensed under the MIT License.