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.1

Published

![npm](https://img.shields.io/npm/v/@safe-hand/safe-env-check) ![license](https://img.shields.io/npm/l/@safe-hand/safe-env-check) ![downloads](https://img.shields.io/npm/dm/@safe-hand/safe-env-check)

Downloads

483

Readme

safe-env-check

npm license downloads

A tiny TypeScript library to validate environment variables using a schema with support for:

  • ✅ Type validation
  • ✅ Required & default values
  • ✅ Enum values
  • ✅ Strict mode (no extra env vars)
  • ✅ dotenv integration
  • ✅ Custom error formatting
  • ✅ CLI support

Installation

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

or

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

Features

  • Validate process.env using a schema

  • Strongly typed output (TypeScript)

  • Prevents app startup with invalid configuration

  • Supports CLI for CI/CD and deployment checks

  • Customizable error messages

  • Optional strict mode to disallow unknown variables

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

DATABASE_URL: { type: "string", required: true },
DEBUG: { type: "boolean", default: false },
MODE: { type: "enum", values: ["dev", "prod"] }

Strict Mode

Disallow environment variables that are not defined in the schema.

validateEnv(schema, { strict: true });

If extra variables are found, validation will fail.

Custom Error Formatter

You can control how errors are displayed:

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

Dotenv Support

By default, the library loads .env automatically using dotenv.

Example .env file:

PORT=3000
JWT_SECRET=supersecret
NODE_ENV=development

CLI Usage

Create a schema file called env.schema.js:

module.exports = {
  PORT: { type: "number", required: true },
  NODE_ENV: { type: "enum", values: ["dev", "prod"], default: "dev" },
};

Run validation:

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

License

MIT © Shakhawat Hossain