env file format weirdly - node.js

I'm working on node project, I'm using .env file to hide some of data. The .env file worked normally, and after I added more informations it got some color letters for variable names and it won't detect my variables when i'm using:
mongodb+srv://MONGODB_USERNAME:MONGODB_PASSWORD#cluster0.ekxmb.mongodb.net/MONGODB_DATABASE_NAME?retryWrites=true&w=majority
I'm having following code in my .env file:
MONGODB_PASSWORD = testpassword;
MONGODB_USERNAME = testuser;
MONGODB_DATABASE_NAME = testdbname;
It works when I manually type in app.js file code like this:
"mongodb+srv://testuser:testpassword#cluster0.ekxmb.mongodb.net/testdbname?retryWrites=true&w=majority"
After I added some additional variables code got formed weirdly with colors on variable names.
Note: It worked with code where I'm importing variables from .env file before, but after weird .env format it won't work.

Putting a variable name in a string will just make it say that variable name. In addition, you don't have the environment variables loaded anywhere.
Install the dotenv npm package for processing the .env file. Then, add this to your code to load the config file:
require('dotenv').config();
Create variables for each of your environment variables from process.env.YOUR_VARIABLE_NAME. An easy way to do this is with destructuring:
let {MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE_NAME} = process.env;
Properly insert these variables into the string. You can use template literals for this:
`mongodb+srv://${MONGODB_USERNAME}:${MONGODB_PASSWORD}#cluster0.ekxmb.mongodb.net/${MONGODB_DATABASE_NAME}?retryWrites=true&w=majority`

Related

Setting and Loading variables depending on environment

I need help on how to call specific variables depending on the environment. Currently, the project's root contains .env file that contains all environment variables being used by the project, but I need to load it according to environment
local -> where development happens
staging -> where we test our project
production -> where we deploy our final project
So in my project root, the goal is to use the file that contains specific values for variables depending on the environment where the [project runs. We have 3 files for the environment variables
local.env
INTEGRATION_URL=http://sandbox.api.com
staging.env
INTEGRATION_URL=http://sandbox2.api.com
production.env
INTEGRATION_URL=http://production.api.com
So inside client.py
from dotenv import load_dotenv
load_dotenv(os.getenv('local.env'))
class Client(BaseClient):
def _init_(self):
self.endpoint = os.environ.get('INTEGRATION_URL') // it should be `http://sandbox.api.com` if I run the project locally
But with this implementation, it still loads the default environment variables defined in the .env file. I need to remove the.env file and just used the 3 env files
Any help?
I solved it by removing all variables in .env, and putting a new variable that would specify which file should be called
.env
ENV_PATH=local.env // or prod.env // or staging.env
Then in client.py
from dotenv import load_dotenv
load_dotenv(os.getenv('ENV_PATH'), 'prod.env')// default to prod if not set
class Client(BaseClient):
def _init_(self):
self.endpoint = os.environ.get('INTEGRATION_URL')

Access to ENV variables defined in ${workspaceFolder}/.env files

For a project I need to define the ENVIRO variable (and some others) with the values prod/stage/dev.
This variable is used in .devcontainer/docker-compose.yml, .devcontainer/Dockerfile, some shell scripts and the Python source to set paths and the like.
Therefore I defined the file ${workspaceFolder}/.env which is imported by the Python extension like described here:
ENVIRO=dev
...
To avoid to execute / debug my code in the wrong environment, I wanted to create a little VSC extension, which does nothing else than to show the value of the ENVIRO variable in the Status Bar at the bottom.
Now the problem. In the extensions activate function I don't get access to the ENV variables defined in .env file:
const envValue = process.env["ENVIRO"];
// gives: undefined
In an terminal in the same VSC instance:
echo $ENVIRO
# gives: dev
When I access ENV variables defined by the system (not the .env file), there is no problem to access them in the extension's activate function:
export function activate( context: vscode.ExtensionContext) {
const envValue = process.env["NVM_BIN"];
// gives: '/Users/andi/.nvm/versions/node/v14.15.1/bin'
Is there no way to access this variable?
My suspicion is following:
The Python extension extends the Environment with the variables using the EnvironmentVariableCollection
This adds them to the terminal environment, but prevents access to the variables in other extensions.
Or do I (hopefully) miss something?

changes to .env file not recognized

My .env file contains the following line:
DBENV='REMOTE'
when I was using a local database before I had it set to
DBENV='LOCAL'
But when I try to run my file, it does not recognize the change. It thinks it's still set to 'LOCAL':
In fact, when I delete the .env file altogether it still says that. I assume that means it's looking at some other .env file, but I don't know where.
The .env file is located in the root of my project's directory:
How do I get process.env to look at the correct environment file?
Are you remembering to require your .env file properly?
require('dotenv').config()
Also, are you remembering to restart your server after each change?
You are using the assignment operator (single =) in your if expression. This operator returns the value it assigned and is therefore equivalent to
process.env.DBENV = 'LOCAL'
if ('LOCAL') {
//...
}
which always evaluates to true.
Use comparison (== or ===) instead.

Node.js / Export configuration file

I have the following configuration file in /etc/sysconfig/myconf:
export USER=root
export NODE_DIR=/opt/MyDir
I want to use these setting in my .js file, which located in /opt/myapplication:
var userApp = //USER in /etc/sysconfig/myconf file
var dir = //NODE_DIR in /etc/sysconfig/myconf file
Is there any way to do it without open the file and parse it contents?
As I understand the export should give me the option to read it easily in node.js, but I don't find how (In addition, when I run export -p, I don't see these variables)
EDIT: what I search is equal Node.js's command to source command in Linux (the variables is not environment variables)
If those environment variables are available when you launch the program, you can use process.env. https://nodejs.org/api/process.html#process_process_env

environment variable in a config.properties file

I'm trying to compile a Maven project that has a config.properties file. In the file, I have a set of environment variables that I have to set before compile.
In the config.properties file the variables are called like this:
${sys:rdfstore.host}:${sys:rdfstore.port}/openrdf-sesame/repositories/iserve/rdf-graphs/service
How do I have to set the variable rdfstore.host, and to what value should I set it to?
I have tried to solve this with:
export rdfstore.host="localhost"
However, with this I obtain a msj that is a invalid identifier, because
it has a point "." How can I solve this problem?
You should be confusing environment variables and the set of sytem properties:
The properties exported from your system as you did with the export command are called environment variables and should not contain dots in the name.
Those properties are then refered to using ${env.XXX}, meaning in your case you should change the variable name to:
export RDFSTORE_HOST="localhost"
It can then be referred to as below:
`${env.RDFSTORE_HOST}`
System variables are those introcued in command line when invoking a maven phase, those ones can host dots in their names:
mvn -Drdfstore.host="localhost"
They can be referred to as follows:
${rdfstore.host}
You can find more informations in the maven properties manual.

Resources