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

smart-env-check

v0.1.0

Published

Validate and type-check environment variables at Node.js startup

Downloads

42

Readme

smart-env-check

Validate and type-check Node.js environment variables at startup — zero runtime dependencies, TypeScript-first.

Install

npm install smart-env-check

Quickstart

Load .env in your app (this package does not load dotenv for you), define a schema, and export the validated result:

import "dotenv/config";
import { validateEnv } from "smart-env-check";

export const env = validateEnv({
  DATABASE_URL: "string",
  PORT: { type: "number", default: 3000 },
  DEBUG: { type: "boolean", default: false },
  LOG_LEVEL: { type: "string", optional: true },
});

env is a frozen object with coerced types. Import it anywhere instead of reading process.env directly.

API

Schema shorthand

Required fields use a type string:

validateEnv({
  DATABASE_URL: "string",
  PORT: "number",
  DEBUG: "boolean",
});

Optional and defaults

Use the object form when a variable is optional or has a default:

validateEnv({
  PORT: { type: "number", default: 3000 },
  LOG_LEVEL: { type: "string", optional: true },
});
  • optional: true (no default) → string | undefined when absent
  • default → used when the variable is missing or empty; do not combine with optional: true

options.env

By default, validateEnv reads process.env. Pass a custom source for tests or explicit injection:

const env = validateEnv({ PORT: "number" }, { env: { PORT: "3000" } });

Only keys in the schema are read; extra keys are ignored.

Supported types

| Type | Coercion rules | | --------- | ----------------------------------------------------------------------- | | string | Trim whitespace; returns the trimmed string | | number | Trim, then Number(); rejects NaN and non-numeric strings | | boolean | Trim, then case-insensitive: true/1true, false/0false |

Empty values: "" and whitespace-only strings are treated as absent (missing), not as valid values.

Errors

Validation failures throw EnvValidationError with an issues array:

import { EnvValidationError, validateEnv } from "smart-env-check";

try {
  validateEnv({ PORT: "number" }, { env: { PORT: "not-a-number" } });
} catch (error) {
  if (error instanceof EnvValidationError) {
    console.log(error.issues);
    // [{ key: "PORT", code: "invalid_type", message: "..." }]
  }
}

All schema errors are collected before throwing — no partial results.

Safe logging

Validation errors never include secret env values in message or received for sensitive keys (e.g. SECRET, PASSWORD, TOKEN, API_KEY, DATABASE_URL) or for any string field.

In production, log keys and codes only — not full error messages that might echo user input:

catch (error) {
  if (error instanceof EnvValidationError) {
    logger.error("Env validation failed", {
      issues: error.issues.map(({ key, code }) => ({ key, code })),
    });
  }
}

TypeScript

Return types are inferred from your schema — no manual generics:

const env = validateEnv({
  PORT: "number",
  LOG_LEVEL: { type: "string", optional: true },
});

env.PORT; // number
env.LOG_LEVEL; // string | undefined

Testing

Inject env values instead of mutating process.env:

import { validateEnv } from "smart-env-check";

const env = validateEnv(
  { API_KEY: "string", PORT: "number" },
  { env: { API_KEY: "test-key", PORT: "3000" } },
);

Requirements

  • Node.js >= 20

License

MIT

Contributing

See the docs/ folder for product, API, architecture, and security specifications.