【发布时间】:2019-09-29 04:47:56
【问题描述】:
如文档here 中所述,我将在nestjs 中配置enivrement 变量:
在我的服务构造函数中,我得到了 env 文件路径
import * as dotenv from 'dotenv';
import * as fs from 'fs';
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath))
console.log(this.envConfig)
}
get(key: string): string {
return this.envConfig[key];
}
}
然后在配置模块中设置配置服务
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV}.env`),
},
],
exports: [ConfigService],
})
当前行为
实际上我得到了未定义的 process.env.NODE_ENV 值
预期行为
在 process.env.NODE_ENV 中获取环境变量路径
【问题讨论】:
-
提供
filePath你想要的价值。比如my_test.env,那么你要设置你的NODE_ENV为my_test,我们设置NODE_ENV环境变量值的方法太多了,简单的方法NODE_ENV=my_test npm run start
标签: javascript node.js nestjs dotenv