@martinpang/nestjs-config
v1.2.3
Published
nestjs easy config via node-config
Downloads
6
Readme
@martinpang/nestjs-config
Configure your Nest.js Applications with node-config lib,without configuration,just import and use,so easy and convenient.
This module is build on node-config,support nestjs DI system,very easy to use and same to config lib.
How to use
npm i @martinpang/nestjs-config
//or you can use yarn or pnpm
pnpm add @martinpang/nestjs-config
the config dir is default to node-config ,the workspace rootdir/config,and many times same to ${workspace}/config
The config file type is same to node-config lib,json,json5,yaml,yml,xml ,cson,ts and so on ,details:https://www.npmjs.com/package/config
//This is app.module.ts,and config path dirs in rootdir
//In this case,the config dirs are rootdir/config2,rootdir/config,rootdir/config3
//The config file name is same to config lib,such as default.EXT,development.EXT ...
//Note:path is based on rootdir,'./' means rootdir,config dir will join with rootdir
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from "@martinpang/nestjs-config"; //here
@Module({
imports: [ConfigModule.forRoot({ path: ['config2', 'config', 'config3'] })],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
If you use default config dir,you can also...
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from "@martinpang/nestjs-config"; //here
@Module({
imports: [ConfigModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
So easy to use in providers:
//This is app.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from "@martinpang/nestjs-config"; //here
@Injectable()
export class AppService {
//Inject via contructor,and use anywhere
constructor(private configService: ConfigService) {}
getHello(): string {
console.log(this.configService.get('Your config vars key'));
return 'Hello World!';
}
}
To use different env config file
//start your nestjs app in package.json script,config will merge NODE_ENV.EXT and default.EXT
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:prod":"cross-env NODE_ENV=production nest start",
//PS.You need cross-env lib or manually set NODE_ENV on your system.
The ConfigMoudle can be used in global scope.You just need import once and use anywhere.
License
Nest is MIT licensed.