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

env-typed-guard

v1.1.3

Published

Type-safe environment variable parsing with full TypeScript IntelliSense support

Readme

env-typed-guard

A TypeScript-first library for type-safe environment variable parsing and validation with full IntelliSense and optional logging.


Features

  • 🔒 Type-safe: Full TypeScript support with automatic type inference
  • 📋 Schema-based: Define environment variables with a clear schema
  • Validation: Built-in and custom validators
  • 🎯 IntelliSense: Autocomplete for environment variable names
  • 🔧 Flexible: Supports string, number, boolean, and enum
  • 📝 Error handling: Throw or log warnings based on configuration
  • 🚀 Zero dependencies: Lightweight, focused, and portable with zero dependencies (only dotenv for peer)

⚠️ Note: This package is Node-first. Full type safety works in Node, Nest.js, and Express. For Next.js or browser environments, variables must be exposed via NEXT_PUBLIC_* and are limited to strings. (Not recommended for next js till now)


Installation

npm install env-typed-guard

Quick Start

import defineEnv from 'env-typed-guard';

const env = defineEnv({
  PORT: { type: 'number', defaultValue: 3000 },
  NODE_ENV: {
    type: 'enum',
    validValues: ['development', 'production', 'test'],
    defaultValue: 'development'
  },
  DATABASE_URL: { type: 'string' }, // Required
  DEBUG: { type: 'boolean', defaultValue: false }
});

console.log(`Server running on port ${env.PORT}`);
console.log(`Environment: ${env.NODE_ENV}`);
console.log(`Debug mode: ${env.DEBUG}`);

API

defineEnv(schema, config?)

Parses and validates environment variables based on a schema.

Parameters

  • schema – Object defining environment variables and their types.
  • config – Optional configuration:

Schema

| Property | Type | Required | Description | | -------------- | --------------------------------------- | -------- | ---------------------------------------------------------- | | type | 'string', 'number', 'boolean', 'enum' | ✅ Yes | The type of the environment variable | | defaultValue | same as type | ❌ No | Default value if the variable is not set | | validate | (value) => boolean \| string | ❌ No | Custom validation function; return true or error message | | validValues | string[] | ❌ No* | Required if type is 'enum'; allowed values |

*Only required for enum type variables.


Configuration (config)

| Option | Type | Default | Description | | ----------- | --------- | ------- | ---------------------------------------------------------- | | debugMode | boolean | false | Enable debug logging of parsed environment variables | | throw | boolean | true | Throw errors on validation failure (if false, logs only) |


Types Supported

String

const env = defineEnv({
  API_KEY: { type: 'string', defaultValue: 'dev-key' }
});

Number

const env = defineEnv({
  PORT: { type: 'number', defaultValue: 3000 }
});
// PORT=8080 → parsed as number 8080

Boolean

const env = defineEnv({
  DEBUG: { type: 'boolean', defaultValue: false }
});
// Accepts: 'true', 'false', '1', '0' (case insensitive)

Enum

const env = defineEnv({
  LOG_LEVEL: { 
    type: 'enum', 
    validValues: ['debug', 'info', 'warn', 'error'], 
    defaultValue: 'info' 
  }
});
// Type: 'debug' | 'info' | 'warn' | 'error'

Advanced Usage

Custom Validation

const env = defineEnv({
  PASSWORD: {
    type: 'string',
    validate: (val) => val.length >= 8 || 'Password must be at least 8 chars'
  },
  PORT: {
    type: 'number',
    defaultValue: 3000,
    validate: (val) => (val >= 1000 && val <= 65535) || 'Port must be 1000–65535'
  }
});

Optional vs Required Variables

const env = defineEnv({
  DATABASE_URL: { type: 'string' },        // Required
  CACHE_TTL: { type: 'number', defaultValue: 300 } // Optional
});

Node vs Client (Next.js)

  • Node/Nest.js/Express: Full types supported.
  • Next.js client: Only strings allowed; use NEXT_PUBLIC_* prefix: (Not recommended till now)
const nextConfig = {
  env: {
    NEXT_PUBLIC_API_URL: env.API_URL, // string only
  }
};

Error Handling

try {
  const env = defineEnv({ REQUIRED_VAR: { type: 'string' } });
} catch (err) {
  console.error('Env validation failed:', err.message);
  process.exit(1);
}

// Or disable throwing
const env = defineEnv({ REQUIRED_VAR: { type: 'string' } }, { throw: false });

Debug Mode

const env = defineEnv({
  PORT: { type: 'number', defaultValue: 3000 }
}, { debugMode: true });

// Output:
// Environment variables:
//   PORT=3000 (using default)

Best Practices

  1. Use as const for type literals:
type: 'string' as const
  1. Validate early in your app startup:
const env = defineEnv(schema); // Top of main file
  1. Use meaningful validation messages:
validate: (value) => value > 0 || 'Must be positive'
  1. Provide defaults for optional vars:
TIMEOUT: { type: 'number', defaultValue: 5000 }

Environment Variable Examples

DATABASE_URL=postgres://user:pass@localhost:5432/db
PORT=8080
DEBUG=true
NODE_ENV=production

TypeScript Integration

  • Type inference from schema
  • IntelliSense autocomplete
  • Compile-time type checking
  • Clear error messages

Tested Environments

  • ✅ Node.js
  • ✅ Nest.js
  • ✅ Express.js
  • 🟨 Next.js (server-side only, client limited to strings)

Contributing

Currently, there are no active contribution opportunities. However, if you come across any issues, feel free to raise them in the GitHub Issues section.