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

@harmand66/typesafe-env

v0.1.1

Published

Zero-runtime-error environment variables for TypeScript

Downloads

275

Readme

🔒 typesafe-env

Zero-runtime-error environment variables for TypeScript

npm version CI License: MIT npm downloads

Stop debugging missing env vars in production. @harmand66/typesafe-env validates and types your environment variables at boot time — with zero dependencies and full TypeScript inference.

The Problem

// 💥 Crashes at runtime, not at startup
const port = parseInt(process.env.PORT); // NaN if missing
const db = process.env.DATABASE_URL;     // string | undefined — not safe!

The Solution

import { createEnv } from '@harmand66/typesafe-env';

const env = createEnv({
  PORT:         { type: 'number',  default: 3000 },
  DATABASE_URL: { type: 'string',  required: true },
  DEBUG:        { type: 'boolean', default: false },
});

// ✅ TypeScript knows PORT is a number — not string | undefined
env.PORT + 1        // 3001
env.DATABASE_URL    // string — guaranteed
env.DEBUG           // boolean — never a string

If any required variable is missing or has the wrong type, your app fails immediately at startup with a clear error — not 3 hours later in production.

Install

npm install @harmand66/typesafe-env
# or
pnpm add @harmand66/typesafe-env
# or
yarn add @harmand66/typesafe-env

Usage

Basic

import { createEnv } from '@harmand66/typesafe-env';

const env = createEnv({
  PORT:         { type: 'number',  default: 3000 },
  DATABASE_URL: { type: 'string',  required: true },
  DEBUG:        { type: 'boolean', default: false },
  APP_NAME:     { type: 'string',  default: 'myapp' },
});

With Express

// env.ts — validate once at the top of your app
import { createEnv } from '@harmand66/typesafe-env';

export const env = createEnv({
  PORT:         { type: 'number', default: 3000 },
  DATABASE_URL: { type: 'string', required: true },
  JWT_SECRET:   { type: 'string', required: true },
});

// server.ts
import { env } from './env';
app.listen(env.PORT); // ✅ number, not string

With Next.js

// lib/env.ts
import { createEnv } from '@harmand66/typesafe-env';

export const env = createEnv({
  NEXT_PUBLIC_API_URL: { type: 'string', required: true },
  DATABASE_URL:        { type: 'string', required: true },
  NEXTAUTH_SECRET:     { type: 'string', required: true },
});

What happens on error

❌ typesafe-env validation failed:
  • Missing required env var: DATABASE_URL
  • Missing required env var: JWT_SECRET
  • PORT must be a number, got "abc"

All errors are collected and shown at once — no more fixing one at a time.

API

createEnv(schema, source?)

| Parameter | Type | Default | |-----------|------|---------| | schema | Record<string, EnvFieldConfig> | required | | source | NodeJS.ProcessEnv | process.env |

EnvFieldConfig

| Option | Type | Default | Description | |--------|------|---------|-------------| | type | 'string' \| 'number' \| 'boolean' | required | The expected type | | required | boolean | true | Whether the var is required | | default | matching type | undefined | Fallback value if missing |

Why not just use Zod?

Zod is great, but it's a 57kb dependency with a verbose API for this specific use case. @harmand66/typesafe-env is zero dependencies, laser-focused on env vars, and fits in a single import.

Contributing

PRs and issues are welcome! Please open an issue before submitting large changes.

git clone https://github.com/giannielloemmanuele-lgtm/typesafe-env.git
cd typesafe-env
npm install
npm test

License

MIT © giannielloemmanuele-lgtm