简体   繁体   English

Linux NodeJs.env 文件中的环境变量

[英]Linux Environment variables in NodeJs .env file

I am currently using the dotenv library in my nodejs project, and reactjs project.我目前在我的 nodejs 项目和 reactjs 项目中使用 dotenv 库。

I have a.env file that works perfectly when I have values like the following我有一个 .env 文件,当我有如下值时可以完美运行

PORT=80
API_URL=https://api.com:8080

However, I am trying to make my os environment variables available in this file, for example if I have the following in my.bashrc file:但是,我正在尝试使我的 os 环境变量在此文件中可用,例如,如果我在 my.bashrc 文件中有以下内容:

DATABASE_HOST=localhost
DATABASE_PASSWORD=password

Is it possible to print the os environment variables in the.env file?是否可以在 .env 文件中打印 os 环境变量? Something similar to Spring boot application.properties file like类似于 Spring 启动 application.properties 文件之类的东西

NODEJS_DB_HOST=${DATABASE_HOST}

Any help is appreciated任何帮助表示赞赏

I don't think dotenv lets you do this.我不认为 dotenv 让你这样做。

If you check out dotenv's main.js file , it seems to just populate the process.env based on the string values found in the.env file.如果您查看dotenv 的 main.js 文件,它似乎只是根据在 .env 文件中找到的字符串值填充 process.env。 It doesn't lookup those string values to see if they're defined in your os environment.它不会查找这些字符串值以查看它们是否在您的操作系统环境中定义。

Your best bet may be to just maintain a.env file, or to pass environment variables through the command line - Twilio has a nice example that illustrates this .您最好的选择可能是只维护一个 .env 文件, 或者通过命令行传递环境变量 - Twilio 有一个很好的例子来说明这一点

Variables from your terminal are loaded to the process.env you don't need to add them to .env file, because they are already in process.env .终端中的变量已加载到process.env中,您无需将它们添加到.env文件中,因为它们已经在process.env中。 Idk what you want to achieve with it. Idk 你想用它实现什么。 :-) :-)

You forgot about export in your ~/.bashrc file.您忘记了在~/.bashrc文件中export

  1. Add your variables to ~/.bashrc将变量添加到~/.bashrc
export VARIABLE=value
  1. Restart terminal or load once again your .bashrc :重新启动终端或再次加载您的.bashrc
source ~/.bashrc
  1. Print your process.env and check if the variables are there.打印您的process.env并检查变量是否存在。

You can also check dotenv-expand: https://www.npmjs.com/package/dotenv-expand也可以查看dotenv-expand: https://www.npmjs.com/package/dotenv-expand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM