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

envconfig-kit

v0.2.0

Published

Type-safe environment variables with fail-fast validation

Readme

envconfig-kit

Type-safe environment variables with fail-fast validation. Zero dependencies.

npm version npm downloads build node bundle size license

Why?

  • Built-in .env loading - no need for dotenv
  • Type-safe - full TypeScript inference
  • Zero dependencies - ~3KB minified
  • Fail-fast validation - catches errors at startup
  • Actionable error messages - tells you exactly how to fix issues
  • Framework presets - common patterns for database, redis, auth
  • Secret masking - hide sensitive values in error messages
  • Custom validation - add your own validation logic

Install

npm install envconfig-kit

Quick Start

import { env } from 'envconfig-kit'

const config = env({
  PORT: { type: 'number', default: 3000 },
  API_KEY: { type: 'string', secret: true },
  DEBUG: { type: 'boolean', default: false },
  DATABASE_URL: { type: 'url' },
})

// config.PORT is typed as number
// config.API_KEY is typed as string
// Throws on startup if API_KEY or DATABASE_URL is missing

No need for dotenv - envconfig-kit automatically loads .env files.

Supported Types

| Type | Validates | Example | |------|-----------|---------| | string | Any string | "hello" | | number | Valid number | "3.14"3.14 | | integer | Whole numbers only | "42"42 | | boolean | true/false, 1/0, yes/no | "true"true | | url | Valid URL with protocol | "https://api.example.com" | | email | Valid email format | "[email protected]" | | port | Number between 0-65535 | "3000"3000 | | json | Valid JSON string | '{"key":"value"}'{key: "value"} | | array | Comma-separated values | "a,b,c"["a", "b", "c"] | | enum | One of allowed values | "production" | | regex | Matches pattern | "ABC-123" |

New in v0.2.0

Enum Type

const config = env({
  NODE_ENV: { 
    type: 'enum', 
    values: ['development', 'staging', 'production'] as const,
    default: 'development'
  },
})
// config.NODE_ENV is typed as 'development' | 'staging' | 'production'

Array Type

const config = env({
  ALLOWED_HOSTS: { type: 'array' },  // comma-separated by default
  TAGS: { type: 'array', separator: '|' },  // custom separator
})
// config.ALLOWED_HOSTS is string[]

JSON Type

const config = env({
  FEATURE_FLAGS: { type: 'json' },
})
// Parse JSON from env var: FEATURE_FLAGS='{"darkMode":true}'

Regex Type

const config = env({
  API_VERSION: { 
    type: 'regex', 
    pattern: /^v\d+\.\d+$/ 
  },
})
// Only accepts values like "v1.0", "v2.1"

Secret Masking

const config = env({
  API_KEY: { type: 'string', secret: true },
  DATABASE_PASSWORD: { type: 'string', secret: true },
})
// Error messages show **** instead of actual values

Custom Validation

const config = env({
  PORT: { 
    type: 'number',
    validate: (value) => value >= 1024 && value <= 65535
  },
})
// Throws if PORT is not in valid range

Framework Presets

import { env, presets } from 'envconfig-kit'

const config = env({
  ...presets.server(),      // PORT, HOST, NODE_ENV
  ...presets.database(),    // DATABASE_URL, DATABASE_HOST, etc.
  ...presets.redis(),       // REDIS_URL, REDIS_HOST, etc.
  ...presets.auth(),        // AUTH_JWT_SECRET, etc.
  ...presets.aws(),         // AWS_ACCESS_KEY_ID, etc.
  ...presets.smtp(),        // SMTP_HOST, SMTP_PORT, etc.
  ...presets.cors(),        // CORS_ORIGINS, CORS_METHODS, etc.
})

Custom prefixes:

const config = env({
  ...presets.database('DB_'),  // DB_URL, DB_HOST, etc.
  ...presets.redis('CACHE_'),  // CACHE_URL, CACHE_HOST, etc.
})

Schema Options

env({
  // Required string
  API_KEY: { type: 'string' },
  
  // With default value
  PORT: { type: 'number', default: 3000 },
  
  // Optional (can be undefined)
  OPTIONAL_VAR: { type: 'string', optional: true },
  
  // Secret (masked in errors)
  PASSWORD: { type: 'string', secret: true },
  
  // With transform
  TAGS: { 
    type: 'array',
    transform: (arr) => arr.map(s => s.toUpperCase())
  },
  
  // With custom validation
  COUNT: { 
    type: 'integer',
    validate: (n) => n > 0 && n < 100
  },
})

Error Messages

When validation fails, you get clear errors with fix suggestions:

❌ Environment validation failed:

  API_KEY: Missing required environment variable
  ➜ Fix: Add to .env file
    Example: API_KEY=your-value-here

  PORT: Invalid number: "abc"
  ➜ Fix: Provide a valid number
    Example: PORT=3000

  Run your app with valid environment variables or add them to .env

Built-in .env Loading

envconfig-kit automatically loads environment variables from:

  • .env
  • .env.local
  • .env.${NODE_ENV} (e.g., .env.development, .env.production)

Priority order (highest to lowest):

  1. process.env
  2. .env.local
  3. .env.${NODE_ENV}
  4. .env

CLI Tools

# Initialize envconfig-kit in your project
npx envconfig-kit init

# Validate .env file
npx envconfig-kit check

# Generate .env.example
npx envconfig-kit generate

# Health check
npx envconfig-kit doctor

Export Schema

import { schemaToMarkdown, schemaToJSON, generateDotenvExample } from 'envconfig-kit'

const schema = {
  PORT: { type: 'number', default: 3000 },
  API_KEY: { type: 'string' },
}

// Generate markdown table for docs
console.log(schemaToMarkdown(schema))

// Generate .env.example
console.log(generateDotenvExample(schema))

// Export as JSON
console.log(schemaToJSON(schema))

License

MIT