【发布时间】:2020-02-02 18:51:12
【问题描述】:
模块
@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 次。我不知道为什么。知道这里发生了什么吗?
【问题讨论】:
-
您的
ConfigService只在providers数组中出现一次?没有其他模块再次提供它?你能显示正在导入你的ConfigModuleresp的模块吗?使用你的ConfigService?
标签: nestjs