Node.js / Export configuration file - node.js

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

Related

env file format weirdly

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`

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.

Require file somewhere in the directory node.js

I have a file that is required in many other files, that are on different folders, inside the main directory.
Is there a way to just require the filename without having to write the relative path, or the absolute path? Like require('the_file'). And without having to go to npm and install it?
Create a folder inside your main directory , put the_file.js inside and set the NODE_PATH variable to this folder.
Example :
Let's say you create a ./libs folder within your main directory, you can just use :
export NODE_PATH = /.../main/lib
after that, you can require any module inside this directory using just :
var thefile = require('the_file')
To not have to do that every time, you'd have to add the variable to your .bashrc (assuming you're running a Unix system).
Or you can set a global variable inside your app.js file and store the path of your 'the_file' in it like so :
global.rootPath = __dirname;
Then you can require from any of your files using :
var thefile = require(rootPath+'/the_file')
These are the most convenient methods for me, short of creating a private npm, but there are a few other alternatives that I discovered when looking up an answer to your question, have a look here : https://gist.github.com/branneman/8048520

Can any one explain the usage of Shell::Source perl module or Shell::GetEnv module

I m a beginner in perl. I want to know how to use this module. I read somewhere about this module but not getting its usage.
Actually I've a file which contains some environment paths which needs to be set while running some test(say file name is SET_ENV_TOOL1.csh or SET_ENV_TOOL1.sh) with particular tools.(say TOOL1, TOOL2 etc)
SET_ENV_TOOL1.sh file conatins:
setenv UVM_HOME /u/tools/digital/uvm/uvm-1.1a
setenv VIPP_HOME /u/tools/digital/vipcat_11.30-s012-22-05-2012
setenv VIP_AXI_PATH ${VIPP_HOME}/vips/amba_axi/vr_axi/sv/ #etc.(almost 10-15 paths are need to be set like this)
Everytime while running test, tool might get changed and so environment paths also needs to set to run that tool.
I have to make a perl script which sets these paths before running test. That test will run a command and that command will use these environment paths.
Any help would be greatly appreciated. Thanks !!
Reading and changing environment variables is built-in to Perl, you do not need the modules you mentioned.
$ENV{UVM_HOME} = '/u/tools/digital/uvm/uvm-1.1a';
$ENV{VIPP_HOME} = '/u/tools/digital/vipcat_11.30-s012-22-05-2012';
$ENV{VIP_AXI_PATH} = "$ENV{VIPP_HOME}/vips/amba_axi/vr_axi/sv/";

Resources