Node process.env variables loaded from env.sh - node.js

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;

Related

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

Linux Environment variables in NodeJs .env file

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

Add express to Path in NodeJS

Hi im working on NodeJS with express in Mac OS,
After install:
with brew: https://changelog.com/posts/install-node-js-with-homebrew-on-os-x
Shows me:
/Users/dortiz/.npm-packages/bin/express -> /Users/dortiz/.npm-packages/lib/node_modules/express-generator/bin/express-cli.js
And if I will execute:
daortiz:~ dortiz$ express
-bash: express: command not found
But:
/Users/dortiz/.npm-packages/bin/express
Its working,
Im trying to export a path with
export PATH=$PATH:/Users/dortiz/.npm-packages/bin/
Its success, but if I close and reopen the terminal don't works command
But doesn't works, any one know what I doing wrong?
Using export sets environment variables for current shell only. Once shell is terminated environment variables for that shell no longer exist.
In order to set environment variables for all shell that are initiated you should put them in ~/.bash_profile as pointed by mailo.
You need to add the line to a file that will be sourced on login, for example: ~/.bash_profile

Nodejs module oracled cannot find LD_LIBRARY_PATH

I'm getting the following error in my nodejs script using the oracledb module on a Centos 6 VM:
Error: libclntsh.so.11.1: cannot open shared object file: No such file or directory
We're using the full Oracle client. My google searches have led me to LD_LIBRARY_PATH being the problem. It is set in /etc/profile.d/ in an oracle script, and I can see it in my linux user's env output. But when I check process.env within the node script, LD_LIBRARY_PATH is simply not there. (If I add other variables to my /etc/profile.d script, those test variables appear in process.env)
The other weirdness is that when I run the script as root, the variable gets set properly and the script executes as expected.
So why is LD_LIBRARY_PATH disappearing when my non-root user runs the script?
You didn't explain how you are invoking the script. If you are sudo-ing, do you need to call a shell script that sets LD_LIBRARY_PATH before invoking node?

Resources