0
votes

I am currently using the dotenv library in my nodejs project, and reactjs project.

I have a .env file that works perfectly when I have values like the following

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:

DATABASE_HOST=localhost
DATABASE_PASSWORD=password

Is it possible to print the os environment variables in the .env file? Something similar to Spring boot application.properties file like

NODEJS_DB_HOST=${DATABASE_HOST}

Any help is appreciated

2

2 Answers

0
votes

I don't think dotenv lets you do this.

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. 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.

0
votes

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. Idk what you want to achieve with it. :-)

You forgot about export in your ~/.bashrc file.

  1. Add your variables to ~/.bashrc
export VARIABLE=value
  1. Restart terminal or load once again your .bashrc:
source ~/.bashrc
  1. Print your process.env and check if the variables are there.

You can also check dotenv-expand: https://www.npmjs.com/package/dotenv-expand