@edirect/config
Global NestJS configuration module for eDirect applications. Provides environment variable loading via dotenv and a ConfigService for accessing configuration values across the application.
Features
- Loads
.{NODE_ENV}.env file automatically on startup (e.g., .development.env, .production.env)
- Global module — register once, inject
ConfigService anywhere
- Simple
get(key) / getConfig() API
- Dual injection tokens: class token (
ConfigService) and string constant (CONFIG_SERVICE_TOKEN)
Installation
pnpm add @edirect/config
npm install @edirect/config
Usage
Register in your AppModule
import { Module } from '@nestjs/common';
import { ConfigModule } from '@edirect/config';
@Module({
imports: [ConfigModule],
})
export class AppModule {}
Because ConfigModule is decorated with @Global(), you only need to import it once at the root module. ConfigService will be available for injection in all feature modules automatically.
Inject ConfigService
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@edirect/config';
@Injectable()
export class MyService {
constructor(private readonly config: ConfigService) {}
doSomething() {
const dbUrl = this.config.get('DATABASE_URL');
const port = this.config.get('PORT') ?? '3000';
}
}
API
ConfigService
constructor | (filePath?: string) | Optionally pass a .env file path. Automatically called with .{NODE_ENV}.env by the module. |
get | (key: string): string | undefined | Returns the value for the given environment variable key from process.env. |
getConfig | (): IStringMapper | Returns all environment variables as a Record<string, string>. |
CONFIG_SERVICE_TOKEN
An alternative injection token (string constant) for ConfigService. Useful when you need to inject by token rather than by class:
import { Inject } from '@nestjs/common';
import { CONFIG_SERVICE_TOKEN, ConfigService } from '@edirect/config';
constructor(@Inject(CONFIG_SERVICE_TOKEN) private config: ConfigService) {}
Environment File Convention
The module automatically loads .{NODE_ENV}.env from the working directory:
development (default) | .development.env |
staging | .staging.env |
production | .production.env |
test | .test.env |
Example .development.env:
PORT=3000
DATABASE_URL=mongodb://localhost:27017/mydb
REDIS_URL=redis://localhost:6379
LOGS_LEVEL=debug
Note: Never commit .env files containing sensitive credentials to version control.
Exports
export { ConfigModule } from './config/config.module';
export { ConfigService } from './config/config.service';
export { CONFIG_SERVICE_TOKEN } from './config/config.constants';
export type { IStringMapper } from './config/config.interfaces';