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

safe-env-getter

v0.1.9

Published

[![npm version](https://img.shields.io/npm/v/safe-env-getter.svg)](https://www.npmjs.com/package/safe-env-getter) [![npm downloads](https://img.shields.io/npm/dm/safe-env-getter.svg)](https://www.npmjs.com/package/safe-env-getter) [![License](https://img.

Readme

Safe Env Getter

npm version npm downloads License

Type-safe environment variable getter for Node.js. Reads and parses process.env with a spec (string, number, boolean, enum), optional defaults, and clear errors when required variables are missing or invalid.

Features

  • Typed specs: string, number, boolean, and enum with TypeScript inference
  • Optional defaults: Fallback values when the variable is unset or empty
  • Strict validation: Throws with clear messages for missing or invalid values
  • Boolean parsing: Accepts 1, true, yes, on (case-insensitive) as true
  • Enum constraint: Restricts values to a fixed set of choices

Installation

npm

npm install safe-env-getter

yarn

yarn add safe-env-getter

Usage

import { SafeEnvGetter, SafeEnvType } from 'safe-env-getter';

// String (spec omitted → defaults to SafeEnvType.String; throws if missing)
const nodeEnv = SafeEnvGetter.getEnv('NODE_ENV');

// String with default (3rd arg: options)
const logLevel = SafeEnvGetter.getEnv('LOG_LEVEL', SafeEnvType.String, { default: 'info' });

// Number with default
const port = SafeEnvGetter.getEnv('PORT', SafeEnvType.Number, { default: 3000 });

// Boolean (parses 1/true/yes/on as true)
const debug = SafeEnvGetter.getEnv('DEBUG', SafeEnvType.Boolean, { default: false });

// Enum (required)
const mode = SafeEnvGetter.getEnv('MODE', SafeEnvType.Enum(['read', 'write']));
// Enum with default
const modeWithDefault = SafeEnvGetter.getEnv('MODE', SafeEnvType.Enum(['read', 'write']), { default: 'read' });

Options

SafeEnvGetter.getEnv(key, spec?, options?) takes:

| Argument | Required | Description | |----------|----------|-------------| | key | Yes | Environment variable name (e.g. "PORT", "NODE_ENV"). | | spec | No | Type spec; defaults to SafeEnvType.String when omitted. Use SafeEnvType.String, SafeEnvType.Number, SafeEnvType.Boolean, or SafeEnvType.Enum(choices). | | options | No | Optional object. Use { default: value } to provide a fallback when the variable is missing or empty. |

Spec types:

| Spec | Shape | Description | |------|--------|-------------| | string | { type: 'string' } | Raw string value. | | number | { type: 'number' } | Parsed with Number(); throws if NaN. | | boolean | { type: 'boolean' } | 1/true/yes/on (case-insensitive) → true, else false. | | enum | { type: 'enum', choices: readonly T[] } | Value must be in choices; throws otherwise. |

Errors:

  • If the variable is missing or empty and options.default is not set: Missing required environment variable: <key>.
  • For number, invalid values: Env <key>: expected number, got "<raw>".
  • For enum, invalid values: Env <key>: must be one of [choice1, choice2, ...].

Requirements

  • Node.js >= 20.0.0

License

This project is licensed under the Apache-2.0 License.