0PricingLogin
NestJS Enterprise Backend APIs · Lesson

Namespaced Config with registerAs

Group related settings into typed config namespaces and inject them with ConfigService.get.

The Problem With Flat Config

As an API grows, dumping every setting into one giant ConfigService namespace becomes unmanageable. You end up with calls like config.get('DB_HOST'), config.get('REDIS_PORT'), and config.get('JWT_SECRET') scattered everywhere with no grouping and no type safety.

  • No structure — database, cache, and auth keys all live at the same flat level.
  • No typesget() returns unknown or a loosely typed value.
  • Hard to refactor — renaming a key means hunting raw strings across the codebase.

NestJS solves this with namespaced configuration via the registerAs helper from @nestjs/config.

Introducing registerAs

registerAs(token, factory) creates a configuration factory bound to a namespace token. The factory reads from process.env and returns a typed object grouping related settings together.

Each namespace becomes its own logical unit — database, jwt, redis — that you can register, inject, and test independently.

import { registerAs } from '@nestjs/config';

export default registerAs('database', () => ({
  host: process.env.DB_HOST ?? 'localhost',
  port: parseInt(process.env.DB_PORT ?? '5432', 10),
  username: process.env.DB_USER ?? 'postgres',
  password: process.env.DB_PASSWORD ?? '',
  name: process.env.DB_NAME ?? 'app',
}));

All lessons in this course

  1. Schema-Validated Env with Joi and forRoot
  2. Namespaced Config with registerAs
  3. Loading Secrets from HashiCorp Vault and AWS SSM
  4. Per-Environment Config and Safe Defaults
← Back to NestJS Enterprise Backend APIs