0

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.

7
  • 2
    Those are shell built-ins, not executables. Sounds like an XY problem though, what exactly are you trying to do? Commented Jul 3, 2015 at 16:50
  • Yes they are shell built ins, i want to set and unset env variables. Can i add bash or sh before the command and run via spawn? Commented Jul 3, 2015 at 17:34
  • 1
    Have you seen the env option for spawn? Commented Jul 3, 2015 at 17:36
  • Nope, thanks for the tip, env looks like a binary, but i wish there was a way to execute commands, or should i write a shell script and execute that instead incase i need to? Commented Jul 3, 2015 at 17:43
  • I meant the env option for spawn, not the env command. Anyway, if you want to execute shell commands you could use child_process.exec instead of spawn, as exec runs the command in a shell environment. Commented Jul 3, 2015 at 17:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My usecase would be setting the env variable and making 'request' module use the env variable. npmjs.com/package/request#proxies . According to the documentation request respects env variables like ' http_proxy' , I already set the env variable in /etc/profile file, but that would require a restart of the device.
@monotheist and you're running request from a separate process that doesn't get started from your UI server, I assume? Why not write the proxy setting to a (JSON) file and load that from the process you're calling request from?
No request is from the same process, so would that mean 'request' will honour the env variable after beign set. I already write the proxy settings to a json file. Request also takes 'proxy' paramters, so if it doesnt work by setting env variables i will just read it off the json and pass it to request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.