Node.js Global variables from 2 different scripts would they conflict? - node.js

For Node.js global variable, if i have a script running on port 3000, and another exact same script running on 3001, would the global variable conflict each other? Or if they are in totally different environment?
My situation is this, i have an AWS EC2 instance, and 2 script running on it, they are exactly the same script and both scripts has a lot of global variables like this global[version1data] so if i run both of the script, does would my data get corrupted as if they are running in the same script?

In you case they won't conflict because they are global per process. Think about the process, which is a global object, and within each Node.js app/program you can get different values for process.env, for example

Every time you run "node something.js", you are creating a new process with a different environment. So... no, the global variables will not conflict.

Related

Node.js: How to read variables from the system?

I'm completely newbie with Docker and to make a deploy in production I need to read the "environment" variables instead of a file or instead of the package.json script line from the operating system of the container.
I know how to read variables from a .env file or from the script line but I don't know how to do it from the system and I don't know if its possible to read these variables from the system.
How can I do that? Is it possible?
The process doesn't change. You still use the below to read environment variables that the process is running in. Reference for Node.
const envVariable = process.env.NAME_OF_VARIABLE;
Setting the variable in a Dockerfile can be done using the below. Docs for this are here.
ENV <key>=<value> ...
Things get more complicated when using things like LXC or Kubernetes though.

Env Variables in NodeJS

I'm new to NodeJS and trying to understand the internal working of the system. Normally we create a .env file or some other configuration file to keep and manage secrets. The same environment values can be kept at the system level like using "export" command on mac.
what I'm trying to understand is how NodeJS loads and reads these value when we start the program either from a configuration file or from system itself.
You can dig through the NodeJS source code to actually see how the environment is provided to NodeJS, e.g. here through the RealEnvStore class. As you can see uv_os_getenv abstracts access to the env depending on the actual operation system.
At least on unix systems uv_os_getenv uses the environ variable which basically references all the environment variables that are made available to the node process as you can see here.

Change ENV variables at runtime

Is it possible to inject/change the current enviroment variables in an already loaded and started NodeJS process?
Exposing an Interface within the application is not an option, restarting is also not a valid option.
The process is running inside a docker container, requiring a specific NodeJS Version is possible.
EDIT: The change must be done from outside the application source so doing process.env.ENV_VAR = "new env" is not possible.
It isn't possible to modify the env vars of a running process. This isn't unique to NodeJS processes. It's just how env vars work on UNIX like operating systems. The vars live within the address space of the process. And while they are, typically, initially placed at a well known location near the top of the stack the current vars are likely to be at an arbitrary address in the heap. Env vars are intentionally private to each process. So unless the program provides an API for changing its env vars you can't modify them once the program is running.
You should use a redis store shared between containers that has stored the env.
redis node repo - redis listen for changes

Changing environment variables of terminal from node.js

I'm working on a simple node.js CLI that provides functionality for (MF) authentication for AWS. This requires on-calling the AWS CLI and updating environment variables in the current terminal (in order to allow direct calls to the aws cli or other consumers of the aws cli to call it).
Is there any way to update the environment variables so they can be made available to the calling terminal?
Neither process.env['KEY'] = 'VALUE' nor require('child_process').exec('export KEY=VALUE', ...) works, as they seem to be isolated to the current or child process and not the executing terminal.
Solutions or alternatives that still allow a simple call through the node.js CLI would be much appreciated! I've already got a bash script that does this, but wanted to allow calls through node so that it's easier to install and provides a consistent interface with other functionality.
OS: currently linux, possibly also windows later.
Node Version: 10+
If I understand your question correctly you are trying to pass environment variables from a child process back to a parent process. This is not possible. However it might be worth a try to spit out the environment variable as a string output and use this to source the environment in the parent process.

How to run Linux/Ubuntu commands(not executables) from Nodejs

I have been running executables using spawn in nodejs all this while, now when i am trying to use spawn to run ubuntu commands like unset, export etc, they dont seem to work. I guess cause it is looking for executables.
I even tried exec, that does not seem to work too. Any tips?
I have an ubuntu device running node, from the UI i need to set/unset env variables for proxy, e.g. http_proxy and no_proxy. Apart from export what other way can i do it via node? The env variables should be set system wide not just for the current process.
Environment variables only exist in memory and are local to a process. For any running process, only the process itself can make changes to the set of environment variables "belonging" to that process, but those changes will not propagate to existing child or parent processes.
In short: you cannot change an environment variable that will apply to all processes on your system (not even from a regular shell).
You can only set an environment variable so it becomes available for newly created child processes (child processes by default inherit the set of environment variables from their parent), but that's about it.
If you have control over how the processes that require those specific environment variables are started, you could write the value for those variables to a file (from Node) and source that file right before the other processes are started, but it really depends on the actual use case whether this is a usable option.

Resources