Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

envnest

broisnischal18MIT0.0.14TypeScript support: included

Typesafe environment variables for NestJS using zod.

nestenv, nest env, .env, nestjs, zod, @nestjs/env, env, environment, variables, typesafe, dotenv, config, nestjs-env, validation, validator, env-variables, env-config, env-nestjs, env-zod, env-nestjs-zod, env-nestjs-zod-validation, typesafe-env

readme

envnest

envnest is a TypeScript library for NestJS that provides type-safe environment variable validation and access using Zod schemas.

Installation

npm install envnest

Usage

1. Define your environment schema

Create a file to define your environment schema using Zod:

// src/config/env.config.ts
import { createEnvConfig, z } from "envnest";

const envSchema = z.object({
  NODE_ENV: z.enum(["development", "production"]).default("development"),
  PORT: z.string().transform(Number).default("3000"),
  DATABASE_URL: z.string().url(),
  TEST: z.string(),
});

export const envService = createEnvConfig(envSchema);

declare global {
  namespace NodeJS {
    interface ProcessEnv extends z.infer<typeof envSchema> {}
  }
}

2. Configure NestJS ConfigModule

In your app.module.ts file, set up the ConfigModule using the envService:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { envService } from './config/env.config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      validate: envService.validateConfig,
    }),
    // other imports...
  ],
  // ...
})
export class AppModule {}

3. Use the validated environment variables

You can now use the validated environment variables in your services:

import { Injectable } from '@nestjs/common';
import { envService } from './config/env.config';

@Injectable()
export class AppService {
  getHello(): string {
    const url = envService.config.get("DATABASE_URL");
    return `testing ${url} ${process.env.PORT}`;
  }
}

Features

  • Type-safe environment variable access
  • Automatic validation of environment variables
  • Default values support
  • Global type augmentation for process.env
  • Built-in presets for common configurations
  • Custom preset support

API Reference

  • createEnvConfig(schema: ZodSchema): Creates a configuration service with validation
  • envService.config: Typed ConfigService instance
  • envService.validateConfig: Validation function for use with NestJS ConfigModule

License

This project is licensed under the MIT License. See the LICENSE file for more details.

changelog

nestenv

0.0.13

Patch Changes

  • fc1d997: Docs-updated

0.0.12

Patch Changes

  • bcf2a22: Fixed issue of package not importing, and updated the package readme.

0.0.11

Patch Changes

  • 3becc43: keywords

0.0.10

Patch Changes

  • 2e0e3ae: ready-for-prod

0.0.9

Patch Changes

  • 600e74f: strict-required

0.0.8

Patch Changes

  • c9c302f: Fixed optional env issue

0.0.7

Patch Changes

  • 84fbec2: Number-issuefixed

0.0.6

Patch Changes

  • 0e39e36: fix-changes

0.0.5

Patch Changes

  • 48b52dc: improvement

0.0.4

Patch Changes

  • 3b60d87: fix

0.0.3

Patch Changes

  • 1c17fbe: import-fix

0.0.2

Patch Changes

  • 85cf8a7: init-project
  • 0b32df9: chore:module-fix

0.0.1

Patch Changes

  • a32acea: publish

0.0.2

Patch Changes

  • 7ce17ad: init-publish

0.0.2

Patch Changes

  • a6c0a47: init package

1.0.1

Patch Changes

  • d5ecb1c: nestjs zod validator