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-tonic

v1.0.0

Published

Zero‑config, type‑safe environment variable loader for TypeScript projects.

Readme

env-tonic 🧪

Zero‑config, type‑safe environment variable loader for TypeScript projects.

npm version License: MIT

Features

  • 🚀 Zero-config: Works out of the box with sensible defaults
  • 🛡️ Type-safe: Full TypeScript support with Zod schema validation
  • 📁 Flexible: Supports custom .env file paths
  • 🔄 Smart merging: OS environment variables override .env file values
  • Clear errors: Detailed validation error messages
  • 🎯 Lightweight: Minimal dependencies

Installation

npm install env-tonic zod

Quick Start

  1. Create your environment schema:
import { z } from 'zod';
import { loadEnv } from 'env-tonic';

const envSchema = z.object({
  ENV: z.enum(['development', 'production', 'test']).default('development'),
  PORT: z.string().transform(Number).pipe(z.number().int().positive()),
  DATABASE_URL: z.string().url(),
  API_KEY: z.string().min(1),
  DEBUG: z.string().transform(val => val === 'true').pipe(z.boolean()).optional(),
});

// Load and validate your environment
const env = await loadEnv(envSchema);

// Now use your fully typed environment variables
console.log(`Server running on port ${env.PORT}`);
console.log(`Database: ${env.DATABASE_URL}`);
  1. Create your .env file:
ENV=development
PORT=3000
DATABASE_URL=postgresql://localhost:5432/myapp
API_KEY=your-secret-key
DEBUG=true

API Reference

loadEnv(schema, options?)

Loads and validates environment variables according to your Zod schema.

Parameters

  • schema (ZodTypeAny): A Zod schema defining your environment variables
  • options (optional):
    • path?: string - Custom path to your .env file (default: process.cwd() + '/.env')
    • encoding?: BufferEncoding - File encoding (default: system default)

Returns

Promise that resolves to your typed environment object matching the schema.

Throws

Error with detailed validation messages if any environment variable fails validation.

Usage Examples

Basic Usage

import { z } from 'zod';
import { loadEnv } from 'env-tonic';

const schema = z.object({
  PORT: z.string().transform(Number),
  ENV: z.string(),
});

const env = await loadEnv(schema);
console.log(`Running on port ${env.PORT}`);

Advanced Schema with Transformations

const schema = z.object({
  // String to number transformation
  PORT: z.string().transform(val => parseInt(val, 10)),
  
  // Enum validation
  LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
  
  // Boolean transformation
  ENABLE_HTTPS: z.string()
    .transform(val => val.toLowerCase() === 'true')
    .pipe(z.boolean()),
  
  // Array transformation (comma-separated)
  ALLOWED_ORIGINS: z.string()
    .transform(val => val.split(',').map(s => s.trim()))
    .pipe(z.array(z.string().url())),
  
  // Optional with default
  MAX_CONNECTIONS: z.string()
    .transform(Number)
    .pipe(z.number().int().positive())
    .default('100'),
});

Custom .env File Path

const env = await loadEnv(schema, {
  path: './config/.env.production'
});

Error Handling

try {
  const env = await loadEnv(schema);
  // Use your validated environment
} catch (error) {
  console.error('Environment validation failed:', error.message);
  process.exit(1);
}

CLI Usage

env-tonic includes a CLI tool for testing your environment configuration:

# Install globally or use npx
npm install -g env-tonic

# Test your environment (update src/index-cli.ts with your schema first)
npx envtonic

Environment Variable Priority

env-tonic follows this priority order (highest to lowest):

  1. OS Environment Variables - process.env
  2. .env File - Your local environment file

This means you can override any .env file value by setting it as an OS environment variable.

Common Patterns

Database Configuration

const dbSchema = z.object({
  DATABASE_HOST: z.string(),
  DATABASE_PORT: z.string().transform(Number).pipe(z.number().int().positive()),
  DATABASE_NAME: z.string(),
  DATABASE_USER: z.string(),
  DATABASE_PASSWORD: z.string(),
  DATABASE_SSL: z.string().transform(val => val === 'true').pipe(z.boolean()).default('false'),
});

Service Configuration

const serviceSchema = z.object({
  SERVICE_NAME: z.string(),
  SERVICE_VERSION: z.string().regex(/^\d+\.\d+\.\d+$/),
  HEALTH_CHECK_INTERVAL: z.string().transform(Number).pipe(z.number().int().positive()).default('30'),
  GRACEFUL_SHUTDOWN_TIMEOUT: z.string().transform(Number).pipe(z.number().int().positive()).default('10'),
});

TypeScript Integration

env-tonic provides full TypeScript support. Your environment object will be properly typed based on your Zod schema:

const schema = z.object({
  PORT: z.string().transform(Number),
  DEBUG: z.string().transform(val => val === 'true').pipe(z.boolean()),
});

const env = await loadEnv(schema);
// env.PORT is typed as number
// env.DEBUG is typed as boolean

Error Messages

When validation fails, env-tonic provides clear, actionable error messages:

⚠️  Env validation failed:
PORT: Expected number, received nan
DATABASE_URL: Invalid url
API_KEY: String must contain at least 1 character(s)

Requirements

  • Node.js >= 14.0.0
  • TypeScript (if using TypeScript)
  • Zod >= 3.0.0

Contributing

  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

Testing

npm test

Building

npm run build

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Sinan Sonmez (Chaush) - [email protected]

Repository

https://github.com/sinansonmez/env-tonic