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

v1.0.0

Published

A tiny, zero-dependency utility to safely load, validate, and manage environment variables in Node.js.

Downloads

5

Readme

safe-env

A tiny, zero-dependency utility to safely load, validate, and manage environment variables in Node.js.

It ensures your application starts with a valid configuration, failing fast with clear error messages if any required variables are missing or malformed.

Problem

Manually checking process.env is error-prone and repetitive.

// The old way
const PORT = process.env.PORT || 3000;
if (!process.env.API_KEY) {
  throw new Error("FATAL: Missing API_KEY!");
}

This approach leads to scattered checks, inconsistent error handling, and silent bugs in production.

Solution

safe-env provides a clean, centralized schema to define your environment configuration.

// The new way with safe-env
const config = safeEnv({
  API_KEY: { type: 'string', required: true },
  PORT: { type: 'number', default: 8080 },
});

Features

  • Schema Validation: Define required and optional variables in one place.
  • Fail-Fast: Throws a clear, readable error for missing variables.
  • 🔄 Type Casting: Automatically converts variables to number, boolean, and json.
  • 🤫 .env Support: Automatically loads variables from a .env file for local development.
  • 💡 Zero Dependencies: Tiny, fast, and secure. No external packages.
  • 🔒 Immutable: The returned config object is frozen to prevent runtime mutations.
  • 🚀 TypeScript Ready: Fully typed with intelligent inference.

Installation

npm install safe-env-manager

Usage

  1. Create a configuration file, for example src/config.ts.
  2. Define your schema and export the validated config.

src/config.ts

import safeEnv from 'safe-env-manager';

const config = safeEnv({
  NODE_ENV: { type: 'string', default: 'development' },
  PORT: { type: 'number', default: 3000 },
  HOST: { type: 'string', default: '0.0.0.0' },

  // Required variables
  JWT_SECRET: { type: 'string', required: true },
  DATABASE_URL: { type: 'string', required: true },
  
  // Typed variables
  ENABLE_FEATURE_X: { type: 'boolean', default: false },
  CORS_ORIGINS: { type: 'json', required: true },
});

export default config;

If JWT_SECRET is missing, the application will immediately crash on startup with:

❌ Missing required environment variable(s): JWT_SECRET

Now you can import your strongly-typed config anywhere in your app:

src/server.ts

import config from './config';

console.log(`Server running in ${config.NODE_ENV} mode.`);
// config.PORT is a 'number', not a 'string | undefined'
app.listen(config.PORT, config.HOST, () => {
  console.log(`Server listening on http://${config.HOST}:${config.PORT}`);
});

CLI Checker

You can verify your environment without running the full application. This is great for CI/CD pipelines or deployment scripts.

  1. Create a safe-env.config.js file in your project root:

safe-env.config.js

const schema = {
  JWT_SECRET: { type: 'string', required: true },
  DATABASE_URL: { type: 'string', required: true },
  PORT: { type: 'number', default: 3000 },
};

module.exports = { schema };
  1. Run the checker:
npx safe-env-check
  • On success, it will print: ✅ All required environment variables are present and valid. and exit with code 0.
  • On failure, it will print the missing variables error and exit with code 1.

API

safeEnv(schema)

schema

An object where each key corresponds to an environment variable. The value is an options object with the following properties:

  • type: ('string' | 'number' | 'boolean' | 'json') The target type for the variable.
  • required: (boolean, optional) If true, the process will exit if the variable is not defined.
  • default: (any, optional) A fallback value to use if the variable is not set. The type should match the specified type.