Building a Type-Safe Environment Module in NestJS

January 22, 2025

Introduction

In this tutorial, we will learn how to create a dedicated environment module with type safety using TypeScript and NestJS configuration validation.

Prerequisites

Setup

npm install zod

First, let's create our environment validation schema in env.ts:

import { z } from 'zod';
 
export const envSchema = z.object({
  PORT: z.number().default(3000),
  DATABASE_URL: z.string(),
  JWT_SECRET: z.string(),
});
 
export type Env = z.infer<typeof envSchema>;

Before we start creating our environment module, let's install the necessary dependencies:

npm install --save @nestjs/config

Now, let's create our environment module in env.module.ts:

import { Module,Global } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { envSchema } from './env';
import { EnvService } from './env.service';
 
@Global() // Important! This module should be global to ensure it is available in all modules.
@Module({
  imports: [
    ConfigModule.forRoot({
      validate: (config) => envSchema.parse(config),
    }),
  ],
  providers: [EnvService],
  exports: [EnvService],
})
export class EnvModule {}

Finally, create env.service.ts:

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Env } from './env';
 
@Injectable()
export class EnvService {
  constructor(private configService: ConfigService<Env>) {}
 
  get<T extends keyof Env>(key: T): Env[T] {
    return this.configService.get(key);
  }
}

Now we can use the EnvService in any other module:

import { Injectable } from '@nestjs/common';
import { EnvService } from './env.service';
 
@Injectable()
export class MyService {
  constructor(private envService: EnvService) {}
 
  getEnv() {
    return this.envService.get('PORT'); // get is type safe
  }
}

Conclusion

In this tutorial, we learned how to create a dedicated environment module with type safety using TypeScript and NestJS configuration validation. This module can be used to access environment variables in a type-safe way, ensuring that the values are correctly typed and validated.

Resources