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

@blimu/nestjs-env

v0.0.4

Published

Type-safe environment variable management for NestJS using Zod schemas

Downloads

32

Readme

@blimu/nestjs-env

Type-safe environment variable management for NestJS using Zod schemas. Validate and inject environment variables with full TypeScript support.

Installation

npm install @blimu/nestjs-env @nestjs/config zod
# or
yarn add @blimu/nestjs-env @nestjs/config zod
# or
pnpm add @blimu/nestjs-env @nestjs/config zod

Requirements

  • Node.js >= 18.0.0
  • NestJS >= 11.0.0
  • Zod >= 3.0.0

Usage

Basic Setup with Zod Schema

Define your environment variables using a Zod schema and register the module:

import { Module } from '@nestjs/common';
import { EnvModule, ENVIRONMENT_VARIABLES } from '@blimu/nestjs-env';
import { z } from 'zod';

const envSchema = z.object({
  DATABASE_URL: z.string().url(),
  PORT: z.coerce.number().default(3000),
  NODE_ENV: z
    .enum(['development', 'production', 'test'])
    .default('development'),
  API_KEY: z.string().min(1),
});

@Module({
  imports: [EnvModule.register(envSchema)],
})
export class AppModule {}

Inject validated environment variables in your services:

import { Injectable, Inject } from '@nestjs/common';
import { ENVIRONMENT_VARIABLES } from '@blimu/nestjs-env';
import { z } from 'zod';

const envSchema = z.object({
  DATABASE_URL: z.string().url(),
  PORT: z.coerce.number().default(3000),
});

type EnvironmentVariables = z.infer<typeof envSchema>;

@Injectable()
export class AppService {
  constructor(
    @Inject(ENVIRONMENT_VARIABLES)
    private readonly env: EnvironmentVariables
  ) {}

  getDatabaseUrl() {
    return this.env.DATABASE_URL; // Fully typed!
  }
}

Using Class-Based Environment Variables

For better type safety and IDE support, use the createEnvironmentVariables factory:

import { Module } from '@nestjs/common';
import { EnvModule, createEnvironmentVariables } from '@blimu/nestjs-env';
import { z } from 'zod';

const envSchema = z.object({
  DATABASE_URL: z.string().url(),
  PORT: z.coerce.number().default(3000),
  NODE_ENV: z
    .enum(['development', 'production', 'test'])
    .default('development'),
});

const EnvironmentVariables = createEnvironmentVariables(envSchema);

@Module({
  imports: [EnvModule.register(EnvironmentVariables)],
})
export class AppModule {}

Inject the class directly in your services:

import { Injectable } from '@nestjs/common';
import { EnvironmentVariables } from './app.module';

@Injectable()
export class AppService {
  constructor(private readonly env: EnvironmentVariables) {}

  getDatabaseUrl() {
    return this.env.DATABASE_URL; // Fully typed with class instance!
  }
}

Features

  • Type-safe: Full TypeScript support with inferred types from Zod schemas
  • Validation: Automatic validation of environment variables on application startup
  • Default values: Support for default values via Zod
  • Environment files: Automatic loading of .env and .env.{NODE_ENV} files
  • Class-based: Optional class-based injection for better IDE support
  • Global module: Register once, use anywhere in your application
  • Production-ready: Ignores .env files in production (uses system environment variables)

Environment File Loading

The module automatically loads environment files in the following order:

  1. .env.{NODE_ENV} (e.g., .env.development, .env.production)
  2. .env

Files are loaded relative to the current working directory (process.cwd()).

Note: In production (NODE_ENV=production), .env files are ignored and only system environment variables are used.

Schema Validation

All environment variables are validated against your Zod schema on application startup. If validation fails, the application will not start and you'll get a clear error message indicating which variables are missing or invalid.

Example Schema with Validation

import { z } from 'zod';

const envSchema = z.object({
  // Required string
  DATABASE_URL: z.string().url('DATABASE_URL must be a valid URL'),

  // Number with coercion and default
  PORT: z.coerce.number().int().positive().default(3000),

  // Enum with default
  NODE_ENV: z
    .enum(['development', 'production', 'test'])
    .default('development'),

  // Optional string
  API_KEY: z.string().optional(),

  // Boolean with coercion
  ENABLE_LOGGING: z.coerce.boolean().default(true),

  // Array of strings
  ALLOWED_ORIGINS: z.string().transform((val) => val.split(',')),
});

API Reference

EnvModule.register(schema | class)

Registers the environment module with a Zod schema or a class created by createEnvironmentVariables.

Parameters:

  • schema: A Zod object schema (z.ZodObject)
  • class: A class created by createEnvironmentVariables()

Returns: DynamicModule

createEnvironmentVariables(schema)

Factory function that creates a class from a Zod schema for better type safety.

Parameters:

  • schema: A Zod object schema (z.ZodObject)

Returns: A class constructor that can be used as a NestJS provider

ENVIRONMENT_VARIABLES

Injection token for accessing validated environment variables when using schema-based registration.

License

MIT