带有 dotenv 和 TypeScript 的环境变量 [英] Environment variable with dotenv and TypeScript

查看:135
本文介绍了带有 dotenv 和 TypeScript 的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个 .env 文件:

TYPE=xxx主机=xxx,端口=xxx,用户名=xxx,密码=xxx,数据库=xxx,

在我的文件中我是这样使用的:

import * as dotenv from "dotenv";dotenv.config();导出 const typeOrmConfig: TypeOrmModuleOptions = {端口:process.env.PORT}

但我只能使用 .env 文件中的 port 变量,我不能使用其余的变量,有人能告诉我为什么我不能使用其余的变量吗?

解决方案

其实你已经定义了.env文件的路径

试试这个

import * as dotenv from "dotenv";dotenv.config({ path: __dirname+'/.env' });

也试试这个

require('dotenv').config({ path: __dirname+'/.env' });

<块引用>

根据需要更改.env文件的路径

参考:https://www.npmjs.com/package/dotenv

I create this .env file:

TYPE=xxx
HOST=xxx,
PORT=xxx,
USERNAME=xxx,
PASSWORD=xxx,
DATABASE=xxx,

in my file I use in this way:

import * as dotenv from "dotenv";

dotenv.config();

export const typeOrmConfig: TypeOrmModuleOptions = {
    port: process.env.PORT
}

but i can use only my port variable from .env file and i cannot use rest of the variables, can someone tell me why i can't use rest of my vars?

解决方案

Actually you have define the path of the .env file

Try like this

import * as dotenv from "dotenv";
dotenv.config({ path: __dirname+'/.env' });

Try this also

require('dotenv').config({ path: __dirname+'/.env' });

Change the path of .env file as required

reference : https://www.npmjs.com/package/dotenv

这篇关于带有 dotenv 和 TypeScript 的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆