Nestjs 配置设置:;路径“;参数必须是未定义的[…]接收类型之一
我沿着这条路走: 模块Nestjs 配置设置:;路径“;参数必须是未定义的[…]接收类型之一,nestjs,Nestjs,我沿着这条路走: 模块 @Module({ providers: [ { provide: ConfigService, useValue: new ConfigService(`development.env`), }, ], exports: [ConfigService], }) export class ConfigModule {} 服务: export interface EnvConfig { [key: string]:
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`development.env`),
},
],
exports: [ConfigService],
})
export class ConfigModule {}
服务:
export interface EnvConfig {
[key: string]: string;
}
export class ConfigService {
private readonly envConfig: EnvConfig;
constructor(filePath: string) {
console.log(filePath);
const config = dotenv.parse(fs.readFileSync(filePath));
this.envConfig = ConfigService.validateInput(config);
}
[...]
每当我运行应用程序时:
> nest start
development.env
[Nest] 10496 - 10/04/2019, 2:16:49 PM [NestFactory] Starting Nest application...
undefined
[Nest] 10496 - 10/04/2019, 2:16:49 PM [ExceptionHandler] The "path" argument must be one of ty
pe string, Buffer, or URL. Received type undefined +14ms
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL.
Received type undefined
该服务似乎被实例化了2次。我不知道为什么。知道这里发生了什么吗?根据我们关于不和谐的讨论,您在
AppModule
中提供ConfigService
,同时也导入ConfigModule
,这导致Nest认为需要重新实例化ConfigService
,但没有构造函数的filePath
变量
从AppModule
的providers
数组中删除ConfigService
将解决此问题。而您的ConfigService
在providers
数组中只出现一次?没有其他模块再次提供它?能否显示正在导入ConfigModule
resp的模块。使用配置服务
?