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

node-env-guard

v1.0.0

Published

Fail-fast environment variable validation for Node.js

Downloads

103

Readme

node-env-guard

npm version License: MIT

Fail-fast environment variable validation for Node.js applications. node-env-guard ensures all required environment variables are present and non-empty at application startup, preventing runtime errors due to missing configurations.

Features

  • Simple and lightweight validation
  • Fail-fast approach - exits immediately if validation fails
  • Clear error messages listing all missing variables
  • Zero dependencies
  • TypeScript support
  • Works with CommonJS and ES modules

Installation

npm install node-env-guard

Quick Start

const { checkEnv } = require('node-env-guard');

// Check for required environment variables at app startup
checkEnv(['DATABASE_URL', 'API_KEY', 'NODE_ENV']);

// If any variable is missing or empty, the app exits with helpful error message
// Otherwise, continue with your application logic

Using with ES Modules

import { checkEnv } from 'node-env-guard';

checkEnv(['DATABASE_URL', 'API_KEY', 'NODE_ENV']);

API

checkEnv(requiredVars: string[]): void

Validates that all specified environment variables are present and non-empty.

Parameters:

  • requiredVars (string[]): Array of environment variable names to validate

Behavior:

  • If all variables are present and non-empty, the function returns normally
  • If any variable is missing or empty, a detailed error message is logged and the process exits with code 1

Example:

const { checkEnv } = require('node-env-guard');

// Your required environment variables
const requiredEnv = [
  'DATABASE_URL',
  'JWT_SECRET',
  'REDIS_HOST',
  'LOG_LEVEL'
];

checkEnv(requiredEnv);

// If execution reaches here, all environment variables are valid
console.log('Environment validation passed! Starting application...');

Error Output

When validation fails, you'll see a clear error message:

========================================
ENV VALIDATION FAILED
========================================

Missing required environment variables:

- DATABASE_URL
- API_KEY

Tip: Check your .env file or deployment configuration

========================================

Usage Examples

Express.js Application

import express from 'express';
import { checkEnv } from 'node-env-guard';

// Validate environment variables first
checkEnv(['PORT', 'DATABASE_URL', 'JWT_SECRET']);

const app = express();
const port = process.env.PORT;

// Rest of your application...
app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

NestJS Application

import { checkEnv } from 'node-env-guard';

checkEnv(['DATABASE_URL', 'JWT_SECRET', 'REDIS_URL']);

// Then bootstrap your NestJS app

Configuration File Approach

Create a config/env.js file:

import { checkEnv } from 'node-env-guard';

const requiredVars = [
  'NODE_ENV',
  'DATABASE_URL',
  'CACHE_URL',
  'SESSION_SECRET',
  'SMTP_HOST',
  'SMTP_PORT'
];

checkEnv(requiredVars);

export const config = {
  nodeEnv: process.env.NODE_ENV,
  databaseUrl: process.env.DATABASE_URL,
  cacheUrl: process.env.CACHE_URL,
  sessionSecret: process.env.SESSION_SECRET,
  smtpHost: process.env.SMTP_HOST,
  smtpPort: process.env.SMTP_PORT
};

Then import in your main file:

import { config } from './config/env.js';

.env File Setup

Create a .env file in your project root:

NODE_ENV=development
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
API_KEY=your_api_key_here
JWT_SECRET=your_jwt_secret_here
REDIS_HOST=localhost
REDIS_PORT=6379

Why node-env-guard?

Running a Node.js application without required environment variables can lead to cryptic errors deep in your application code. This package helps you:

  1. Catch errors early - Validate at startup before any initialization
  2. Clear diagnostics - See exactly which variables are missing
  3. Fail safely - Prevent partial initialization and hard-to-debug issues
  4. Production-ready - Essential for containerized deployments (Docker, Kubernetes)

Best Practices

  1. Validate early - Run checkEnv() as one of the first things in your application
  2. List all requirements - Include every required environment variable
  3. Document in README - Let users know what variables are needed
  4. Use .env.example - Create a template file showing required variables:
NODE_ENV=
DATABASE_URL=
API_KEY=
JWT_SECRET=

TypeScript Support

This package includes full TypeScript type definitions. No additional @types/ packages needed.

import { checkEnv } from 'node-env-guard';

const requiredVars: string[] = ['DATABASE_URL', 'API_KEY'];
checkEnv(requiredVars);

License

MIT © 2024 Sarathkumar

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

Found a bug or have a feature request? Please open an issue on GitHub.

Changelog

v1.0.0

  • Initial release
  • Basic environment variable validation
  • Clear error messaging