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.
Related
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.
I am looking to bind a PCF (Pivotal Cloud Foundry) Service to allow us to set certain api endpoints used by our UI within PCF environment. I want to use the values in this service to overwrite the values in the root directory file, 'config.json'. Are there any examples out there that accomplish this sort of thing?
The primary way to tackle this is to have your application do this parsing. Most (all?) programming languages give you the ability to load environment variables and to parse JSON. Using these capabilities, what you'd want to do is to read the VCAP_SERVICES environment variable and parse the JSON. This is where the platform will insert the information from your bound services. From there you, you have the configuration information so you can configure your app using the values from your bound service.
Manual Ex:
var vcap_services = JSON.parse(process.env.VCAP_SERVICES)
or you can use a library. There's a handy Node.js library called cfenv. You can read more about both of these options in the docs.
https://docs.cloudfoundry.org/buildpacks/node/node-service-bindings.html
If you cannot read the configuration inside of your application, perhaps there's a timing problem and you need the information before your app starts, you can use the platform's pre-runtime hooks.
https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#profile
The runtime hooks allow your application to include a file called .profile which will execute before your application. The .profile file is a simple bash script which can do anything needed to ready your application to be run. The only catch is that this needs to happen very quickly because it must complete before your application is able to start up and your application has a finite amount of time to start (usually 60s).
In your case, you could use jq to parse you values and insert them info your config file, perhaps using sed to overwrite a template value. Another option would be to run a small Node.js script, since your app is using Node.js it should be available on the path when this script runs, to read the environment variables and generate your config file.
Hope that helps!
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.
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.
Is there something like node’s process.env to access environment variables? I want to know in my Foxx code whether it's running in development, test, staging, or production.
Alternatively, is there any way to determine if my Foxx app is running in development-mode?
Yes, we have thought about a getenv wrapper several times, I guess now its time to implement it. It may be in time for 2.5.1, we'll see ;-)
The foxx self debug awareness can be achieved via: applicationContext.isDevelopment
As noted above, arangodb now contains the process.env functionality similar to nodejs.