Linux Environment variables in NodeJs .env file - node.js

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

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.

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.
Add your variables to ~/.bashrc
export VARIABLE=value
Restart terminal or load once again your .bashrc:
source ~/.bashrc
Print your process.env and check if the variables are there.
You can also check dotenv-expand: https://www.npmjs.com/package/dotenv-expand

Related

Is there a configuration file for node js

I have a problem with node and SSL. solution is using --use-openssl-ca option when running node. but I should always run my app with that option.
Is there a configuration file for node.js which I set that option in it?
Answering your question. yes you can attach configuration file for nodejs but there is no global config file.
NODE_OPTIONS='--require "./my path/file.js"'
but this will not make the command line shorter.
if you are willing to add it for one specific project.
then use package.json add a starter script there.
if you want it to be in the current bash. do this (Linux) :
export NODE_OPTIONS=--use-openssl-ca
in windows set NODE_OPTIONS=--use-openssl-ca
if you want default in every bash.
echo 'export NODE_OPTIONS=--use-openssl-ca' >> ~/.bashrc

how to pass environment variables stored in .env file to node script running from shell script?

I am trying to run a node js script from shell script in MAC.
#!bin/bash
/usr/local/bin/node /opt/myprojects/instabot/index.js
It works fine, but the problem is when I try to read environment variables, they show as undefined, I am passing environment variables using .env file in node js script using dotenv package (I can read the environment variables correctly when I run it directly using node index.js)
using this to configure dotenv package require('dotenv').config()
.env file is in KEY=VALUE format
If possible try to move that bash file inside the same directory as the index.js file it will work. Otherwise, you need to export env variables and you can do this permanently by editing your .bashrc file or temporarily using commands like export foo=bar

Issue tying to read environment variable in Python

Hi I'm trying to read an environment variable in python, if I execute my script from console with:
python3 myScript.py everything goes ok, but I need to run this script as a service in Ubuntu, in this case, the script can't get the environment variable. Anyone has past for the same issue? I noticed that when i'm trying to read the mongo URI from environment.
The correct way to achieve that is passing the environment variable or environment file to the service file, in my case I added this line to my service file:
EnvironmentFile = /etc/environment

Node process.env variables loaded from env.sh

I know that you can use a library dotenv to set process.env variables when writing an express app, but at work I use Python and we regularly use shell scripts (env.sh for example) to set environmental variables, especially locally.
So I wrote and then sourced an env.sh script to get credentials for my mongo database but I noticed something strange. When I defined the variables in env.sh, I defined them like so:
#env.sh
export username=blahbblahblah
export password=blahblahbalbha
Then I ran
source env.sh
I checked env and saw the variables listed as expected. But then I entered node and I saw the variables listed in process.env as
user=blablabhablabha\n
password=blabhalbhabl\n
So my question is, why does node include the newline character when bash linux doesn't? It must be that newline in the env.sh file, because I was able to fix this issue by writing my script as follows:
export username=blabhablha; export password=blabhalbahbal;

Where to define the postactivate hook with virtualenvwrapper-win?

I'm using virtualenvwrapper-win and want to use the postactivate hook of virtualenvwrapper to set environment variables. However it seems virtualenvwrapper-win doesn't include a postactivate file, and I haven't been able to get it to work by creating my own. Does anybody know how to get the postactivate hook to work with virtualenvwrapper-win?
I want to include this in postactivate to set an environment variable: SET APP_SETTINGS="example.setting"
I actually solved it myself. You can put any environment variables in the activate.bat file in the Scripts folder of your virtualenv.
If you have already defined your VIRTUALENVWRAPPER_HOOK_DIR variable, just add this line to workon.bat.
call "%VIRTUALENVWRAPPER_HOOK_DIR%\postactivate.bat"
Just make sure you put it above :END

Resources