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

@christian-wechsel/typed-env-handler

v1.0.13

Published

Type-safe environment variable handler with validation and Docker secret support

Readme

typed-env-handler

typed-env-handler is a TypeScript/JavaScript package for simple, secure, and type-safe handling of environment variables. It helps load, validate, and access environment variables in your code with static typing. Starting with version 0.3.0, Docker secrets are also supported.

Why I Built This

Traditional tools like dotenv load environment variables into process.env purely as raw, untyped strings—without verifying whether required variables exist or if data types match. Missing configuration or subtle type conversion bugs (such as "false" evaluating as a truthy boolean) often go unnoticed until runtime in production.

typed-env-handler was created to solve these issues:

  • Fail Fast: Immediately validates all required environment variables at application startup. If any variable is missing or malformed, the process exits early with an informative error message.
  • True Type Safety in TypeScript: Variables are parsed at runtime and provided with static typing in your code—a boolean is a real boolean, a number is a number, and not a raw string.
  • Seamless Docker Deployment: Supports Docker secrets (/run/secrets/*) transparently alongside standard environment variables and automatically masks sensitive secrets in debug logs.

Features

  • Typed Environment Variables: Define and consume environment variables with static typing, including static helper functions for minimal boilerplate.
  • Validation: Ensure all required variables are present and correctly formatted with clear, actionable error messages.
  • Docker Secrets & Masking: Declare variables as secrets (loaded from .env or from Docker at /run/secrets/<name>). Secrets are automatically masked as *** in debug logs.
  • Easy Integration: Seamlessly integrates into existing Node.js and TypeScript projects.
  • Error Handling: Provides precise error messages for missing or invalid configuration values.

Use Cases

Ideal for any Node.js application where configuration must be validated and strongly typed, such as API keys, database credentials, or feature flags. Especially well suited for secure containerized production deployments with Docker.

Example

import { HandleEnv } from "@christian-wechsel/typed-env-handler";

const envHandler = new HandleEnv({
  PORT: HandleEnv.number(),
  NODE_ENV: HandleEnv.string(),
  API_KEY: HandleEnv.string({ 
    secret: { dockerName: "api_key" } // Loaded from /run/secrets/api_key in Docker
  }),
  THIRD_PARTY_SECRET: HandleEnv.string({
    secret: true // Loaded from environment, masked as *** in debug logs
  }),
  DEBUG: HandleEnv.boolean({ defaultValue: false }),
});

const PORT = envHandler.getValue("PORT"); // typed as number
const NODE_ENV = envHandler.getValue("NODE_ENV"); // typed as string
const API_KEY = envHandler.getValue("API_KEY"); // typed as string, loaded from Docker secret
const THIRD_PARTY_SECRET = envHandler.getValue("THIRD_PARTY_SECRET"); // typed as string
const DEBUG = envHandler.getValue("DEBUG"); // typed as boolean

Constructor Options

The second parameter of the constructor is optional and accepts the following settings:

| Option | Type | Default | Description | | --- | --- | --- | --- | | docker.pathSecrets | string | /run/secrets/ | Path where Docker secrets are mounted | | debugLogs | boolean | false | Enables verbose debug logging |

const envHandler = new HandleEnv(
  {
    API_KEY: HandleEnv.string({
      secret: { dockerName: "api_key" },
    }),
  },
  {
    docker: { pathSecrets: "/custom/secrets/path/" },
    debugLogs: true, // Only enable during development
  },
);

Security Note: Debug logs output variable names, paths, and values. To prevent leaks, HandleEnv masks any value flagged with secret (secret: true or secret: { dockerName: "..." }) as ***.

In addition, when NODE_ENV=production, debug logs are always automatically suppressed, regardless of the configuration setting.

Docker Integration

When running inside a Docker container, no .env file is loaded. Instead, all values are read directly from process environment variables (e.g. defined via environment: in Docker Compose). Variables declared as secrets are loaded from the configured secret mount path.

Prerequisites

Set IS_DOCKER=true in your Docker environment. This instructs HandleEnv to skip loading any .env file.

Example docker-compose.yaml

name: my-app
services:
  server:
    image: my-app-image:latest
    environment:
      - NODE_ENV=production
      - IS_DOCKER=true   # Prevents loading a .env file
      - PORT=3000
      - DEBUG=false
    secrets:
      - api_key          # Mounted at /run/secrets/api_key

secrets:
  api_key:
    file: ./secrets/api_key.txt

Example Docker Build Mount

RUN --mount=type=secret,id=api_key,target=/run/secrets/api_key ...

Inside your application, HandleEnv automatically detects the container environment and loads secrets directly from the mounted files.