NestJS Config module based on node config
npm install nestjs-node-config-moduleNode.js Application Configuration organizes hierarchical configurations
for your app deployments.
It lets you define a set of default parameters, and extend them for different deployment environments (development, qa,
staging, production, etc.).
#### Installation
To start using Node Config module, first install the required packages:
``bash`
$ npm i --save config nestjs-node-config-module
#### Overview
To use the config module, add ConfigModule import:
`typescript
import { ConfigModule } from "nestjs-node-config-module";
@Module({
imports: [ConfigModule.forRoot()],
controllers: [],
providers: [],
})
export class AppModule {
}
`
Then you can inject config service:
`typescript
import { ConfigService } from "nestjs-node-config-module";
@Injectable()
export class AppService {
constructor(private configService: ConfigService) {
}
getFoo(): string {
return this.configService.get('foo');
}
}
`
#### Options
Config module accepts optional options parameter, parameters are described below:
nodeEnv | Overrides the value of NODE_ENV |
configDir | Contains the paths to the directories containing your configuration files. |
printConfigSources | If true prints config source on module setup |
strictMode | Strict mode (read more here) | configService | Class that extends default ConfigService, you can use it to have all settings in one place |
You can extend default config service with your one, for example:
`typescript
import { ConfigService } from "nestjs-node-config-module";
@Injectable()
class MyConfigService extends ConfigService {
getFoo(): string {
return this.get
}
}
`
Then you have to register it by passing class to Config Module:
`typescript
import { ConfigModule } from "nestjs-node-config-module";
@Module({
imports: [ConfigModule.forRoot({
configService: MyConfigService
})],
controllers: [],
providers: [],
})
export class AppModule {
}
`
And now it's accessible in nest IoC container:
`typescript
import { MyConfigService } from "./my-config.service";
@Injectable()
export class AppService {
constructor(private configService: MyConfigService) {
}
getFoo(): string {
return this.configService.getFoo();
}
}
`
NOTE: default ConfigService` is still available and can be injected.