In NestJS, managing different configuration environments can be achieved through the following steps:
1. Install the Configuration Library
First, install the @nestjs/config module, which is a configuration management library specifically designed for NestJS.
shellnpm install @nestjs/config
2. Create Configuration Files
Create environment-specific configuration files in the project's root directory. For example, you can have:
.env(default environment).env.development(development environment).env.production(production environment).env.test(test environment)
Example content for the .env file:
dotenvDATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_USER=root DATABASE_PASSWORD=example
3. Load and Use Environment Variables
In your application module (typically AppModule), import ConfigModule and configure it to load the appropriate .env file:
typescriptimport { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ ConfigModule.forRoot({ envFilePath: `.env.${process.env.NODE_ENV}`, isGlobal: true, // Make configuration available globally }), // ...other modules ], // ...controllers, providers, etc. }) export class AppModule {}
This configuration loads the correct environment file based on the NODE_ENV variable. Set this variable in your startup script, for example:
shellNODE_ENV=production npm start
4. Access Configuration Variables
Anywhere in your application, access configuration values using ConfigService:
typescriptimport { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @Injectable() export class DatabaseService { constructor(private configService: ConfigService) {} getDatabaseConfig() { const host = this.configService.get<string>('DATABASE_HOST'); const port = this.configService.get<number>('DATABASE_PORT'); // ...other configurations return { host, port, // ... }; } }
5. Validate and Customize Configuration
Define a configuration object or function to validate and map environment variables. Create a .ts file, such as configuration.ts:
typescriptexport default () => ({ database: { host: process.env.DATABASE_HOST, port: parseInt(process.env.DATABASE_PORT, 10), // ... }, // ... });
Then specify this function in ConfigModule.forRoot:
typescriptConfigModule.forRoot({ load: [configuration], // ... })
6. Separate Environment-Specific Configuration
For advanced scenarios, implement environment-specific logic using ConfigService or dynamic modules to create tailored providers and services.
Example: Using a Custom Configuration Service
For highly specific requirements or asynchronous configuration, create a custom service:
typescript@Module({ imports: [ ConfigModule.forRoot({ // ... useClass: CustomConfigService, }), // ... ], // ... }) export class AppModule {}
Here, CustomConfigService must implement the ConfigService interface and override necessary methods to provide configuration.
By following these steps, you can effectively manage environment-specific configurations in NestJS while maintaining code readability and maintainability.