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-hand/safe-env-check

v1.1.7

Published

TypeScript-first environment variable validator with CLI support

Readme

safe-env-check

npm license downloads TypeScript CI Coverage

Fail fast. Validate early. Deploy safely.

A strict, TypeScript-first environment validator that ensures your application never starts—or deploys—with invalid configuration.

Built for developers who want zero surprises in production and reliable CI/CD validation.


🚀 Quick Start (30 seconds)

import { validateEnv } from "@safe-hand/safe-env-check";

const env = validateEnv({
  PORT: { type: "number", default: 3000 },
  NODE_ENV: {
    type: "enum",
    values: ["development", "production"],
    default: "development",
  },
});

env.PORT; // number
env.NODE_ENV; // "development" | "production"

Why safe-env-check?

Environment variables are fragile and error-prone.

Common problems:

  • Missing required variables → runtime crashes
  • Incorrect types → hidden bugs
  • Unexpected variables → misconfigurations
  • Broken .env files → failed deployments
  • CI/CD pipelines deploying invalid configs

safe-env-check eliminates these risks before your app runs.


🔥 What makes it different?

Unlike general-purpose validators, safe-env-check is built specifically for environment validation workflows:

  • ✅ Fail fast before app startup
  • ✅ Detect unknown variables (strict mode)
  • ✅ Validate configs in CI/CD pipelines
  • ✅ Support multi-service setups with prefixes

Features

  • ✅ Type validation (string, number, boolean, enum)
  • ✅ Required values
  • ✅ Default values
  • ✅ Enum validation
  • ✅ Strict mode (no unknown variables)
  • ✅ Prefix support (multi-environment setup)
  • ✅ dotenv integration
  • ✅ Custom error formatting
  • ✅ Fully type-safe output
  • ✅ CLI support (CI/CD ready)
  • ✅ Designed for production safety

Installation

npm install @safe-hand/safe-env-check

or

yarn add @safe-hand/safe-env-check

Basic Usage

Define a schema

const schema = {
  PORT: { type: "number", required: true },
  JWT_SECRET: { type: "string", required: true },
  NODE_ENV: {
    type: "enum",
    values: ["development", "production"],
    default: "development",
  },
};

Validate environment variables

import { validateEnv } from "@safe-hand/safe-env-check";

const env = validateEnv(schema);

console.log(env.PORT); // number
console.log(env.NODE_ENV); // "development" | "production"

Schema Options

Each environment variable supports the following options:

| Field | Type | Description | | ---------- | --------------------------------------------- | -------------------------------- | | type | "string" or "number" or "boolean" or "enum" | Expected value type | | required | boolean | Whether the variable is required | | default | any | Default value if not provided | | values | string[] | Required for enum type |


Example Schema

const schema = {
  DATABASE_URL: { type: "string", required: true },
  PORT: { type: "number", default: 3000 },
  DEBUG: { type: "boolean", default: false },
  MODE: {
    type: "enum",
    values: ["dev", "prod"],
  },
};

Default Values

If a variable is missing, the default value will be used.

const schema = {
  PORT: { type: "number", default: 3000 },
};

.env

# PORT missing

Result:

PORT=3000

Required Variables

const schema = {
  DATABASE_URL: { type: "string", required: true },
};

If missing:

Error ❌ Environment validation failed:
- DATABASE_URL is required

Boolean Parsing

Accepted values:

true
false

Enum Validation

Restrict values to a predefined list:

NODE_ENV: {
  type: "enum",
  values: ["development", "production", "test"],
}

Invalid values will throw an error.


Strict Mode (recommended)

Prevent unexpected environment variables:

validateEnv(schema, { strict: true });
Unknown env variables: SOME_RANDOM_VAR

Prefix Support

Useful for multi-service or multi-tenant configurations.

Example .env:

API_PORT=3000
API_SECRET=123

Usage:

validateEnv(schema, { prefix: "API_" });

Env File Support

Load custom env files:

validateEnv(schema, {
  envFile: ".env.production",
});

Custom Error Formatter

validateEnv(schema, {
  formatError: (errors) => `Config error:\n${errors.join("\n")}`,
});

Loading Environment Variables

safe-env-check uses dotenv to automatically load .env files.


CLI Usage (CI/CD ready)

Validate environment variables without writing code:

npx safe-env-check env.schema.js --strict

Perfect for:

  • CI pipelines
  • Docker builds
  • Deployment validation

CLI Options

| Flag | Description | | --------------- | ------------------------ | | --schema | Schema file path | | --env-file | Load custom env file | | --strict | Enable strict mode | | --prefix | Env variable prefix | | --format json | Output errors in JSON | | --quiet | Suppress success message |


CLI Examples

npx safe-env-check env.schema.js
npx safe-env-check env.schema.js --strict
npx safe-env-check env.schema.js --prefix API_
npx safe-env-check env.schema.js --env-file .env.production
npx safe-env-check env.schema.js --format json

CI/CD Example

- name: Validate Environment Variables
  run: npx safe-env-check env.schema.js --env-file .env.production --strict

Prevents deployments with invalid configuration.


TypeScript Type Safety

const env = validateEnv(schema);

env.PORT; // number
env.NODE_ENV; // inferred union type

When to Use safe-env-check

  • Node.js APIs
  • NestJS applications
  • Microservices
  • Docker containers
  • CI/CD pipelines
  • Serverless functions
  • Monorepos

Contributing

Contributions are welcome! Open an issue or PR.


Changelog

https://github.com/shshamim63/safe-env-check/releases


License

MIT © Shakhawat Hossain