Config File Setup In NestJs

Abiral Sthapit
2 min readJun 7, 2021
Env Setting File
src/config
├── app
│ ├── app-config.service.ts
│ ├── app-config.controller.ts
│ └── config.ts
└── [...]

Install packages
1. @nestjs/config
2. @hapi/joi
3. @types/hapi_joi

yarn add @nestjs/config @hapi/joi
yarn add -D @types/hapi_joi

Create a .env file in the src of the application and lets assume we have following environment variables.

APP_ENV=dev
APP_NAME=MY APP NAME
APP_PORT=3000

src/config/app/config.ts

The config can be separated according to the config type, but it is rather easier to have only one config file and maintain it across all env dependencies rather than multiple files. In case of multiple files the name app must be unique such as mongo, aws, postgres, etc

import { registerAs } from '@nestjs/config';export default registerAs('app', () => ({
env: process.env.APP_ENV,
name: process.env.APP_NAME,
port: process.env.APP_PORT,
}));

src/config/app/app-config.service.ts

Getter for all the configs available

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class AppConfigService {
constructor(private configService: ConfigService) {}
get env(): string {
return this.configService.get<string>('app.appEnv');
}
get name(): string {
return this.configService.get<string>('app.name');
}
get port(): number {
return this.configService.get<number>('app.port');
}
}

src/config/app-config.module.ts

Validating the env according to nest official: https://docs.nestjs.com/techniques/configuration#schema-validation

import * as Joi from '@hapi/joi';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import config from './config';
import { AppConfigService } from './app-config.service';
@Module({
imports: [
ConfigModule.forRoot({
load: [config],
validationSchema: Joi.object({
APP_ENV: Joi.string()
.valid('dev', 'stage', 'test', 'prod')
.default('dev'),
APP_NAME: Joi.string().default('MY APP'),
APP_PORT: Joi.number().default('3000'),
}),
}),
],
providers: [ConfigService, AppConfigService],
exports: [ConfigService, AppConfigService],
})
export class AppConfigModule {}

Importing config in the AppModule src/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppConfigModule } from './config/app/app-config.module';
@Module({
imports: [AppConfigModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

Importing Config in the Main src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { AppConfigService } from './config/app/app-config.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
//Get app config
const appConfig: AppConfigService = app.get('AppConfigService');
await app.listen(appConfig.port); // <------- From AppConfig
}
bootstrap();

--

--