Skip to main content

Config

@inversifyjs/config loads application configuration into the InversifyJS container. Provide a single config source, optionally validate it with a Standard Schema or custom validator, and inject a typed ConfigService.

Loading config

import {
ConfigContainerModule,
type ConfigService,
configServiceIdentifier,
object,
} from '@inversifyjs/config';
import { Container } from 'inversify';
import { z } from 'zod';

const appConfigSchema = z.object({
DATABASE_URL: z.url(),
PORT: z.coerce.number().default(3000),
});

type AppConfig = z.infer<typeof appConfigSchema>;

export async function bootstrap(): Promise<AppConfig> {
const container: Container = new Container();

await container.loadAsync(
ConfigContainerModule.fromOptions({
source: object({
DATABASE_URL: 'postgres://localhost:5432/app',
PORT: '3000',
}),
validate: appConfigSchema,
}),
);

return container.get<ConfigService<AppConfig>>(configServiceIdentifier).get();
}
tip

Optional helpers for .env and YAML files live in @inversifyjs/config-dotenv and @inversifyjs/config-yaml.