bash 在 Node.js 之外设置全局环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32523101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:33:38  来源:igfitidea点击:

Set global environment variable out of Node.js

node.jsbashspawnenv

提问by

I am trying to set a global environment variable out of my node.js app.

我正在尝试从我的 node.js 应用程序中设置一个全局环境变量。

The goals are:

目标是:

  1. When restarting the APP, the environment variable should still be set
  2. When opening a new shell, it should be usable
  3. If possible: When rebooting, same as 1.
  4. It should work on Linux, Mac OS X (and needs an alternate SET command for windows)
  1. 重启APP时,环境变量还是要设置的
  2. 当打开一个新的shell时,它应该是可用的
  3. 如果可能:重新启动时,与 1 相同。
  4. 它应该适用于 Linux、Mac OS X(并且需要一个用于 Windows 的备用 SET 命令)

Here is what I did:

这是我所做的:

var setEnv = require('child_process')
        .spawn('export GLOBALVARNAME='+my.value,{
          stdio: 'inherit',
          env: process.env
        });

But this causes in

但这导致

{ [Error: spawn export GLOBALVARNAME=foobar ENOENT]
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn export GLOBALVARNAME=foobar',
  path: 'export GLOBALVARNAME=foobar',
  spawnargs: [] }

I didn't test this on Windows, but on Mac OS X (and Linux) the right command on bash is export GLOBALVARNAME=value. For Windows the right command should be SET GLOBALVARNAME=value- isn't it ?

我没有在 Windows 上测试过这个,但在 Mac OS X(和 Linux)上,bash 上的正确命令是export GLOBALVARNAME=value. 对于 Windows,正确的命令应该是SET GLOBALVARNAME=value- 不是吗?

So the main question is: Whats going wrong with the manual working export GLOBALVARNAME=foobar?

所以主要问题是:手动工作出了export GLOBALVARNAME=foobar什么问题?

回答by Hmlth

exportis not a standalone command but a shell builtin that sets the environment variable for the current shell process and its children forked after it is set.

export不是一个独立的命令,而是一个 shell 内置命令,它为当前 shell 进程及其子进程设置环境变量。

You can't set an environment variable for processes that are not descendants of the current process. And under Linux, there is no such thing as system environment variable.

您不能为不是当前进程的后代的进程设置环境变量。而在Linux下,没有系统环境变量这样的东西。

Under Linux, your variable should be set in the init script that spawns your app or in the systemd unit. If you want it to be available in interactive user shells, it should be set in /etc/profileor /etc/profile.d

在 Linux 下,您的变量应该在生成您的应用程序的 init 脚本中或在 systemd 单元中设置。如果您希望它在交互式用户 shell 中可用,则应将其设置为/etc/profile/etc/profile.d

.

.

回答by chicks

As other answers have pointed out, shelling out and changing an environment variable is basically a NO-OP. Either you want to change the environment for your current process and its children or you want to change it for new processes. Editing /etc/profilewill make the change for any new processes as @Hmlth says.

正如其他答案所指出的那样,删除和更改环境变量基本上是NO-OP。您要么想为当前进程及其子进程更改环境,要么想为新进程更改环境。/etc/profile正如@Hmlth 所说,编辑将对任何新进程进行更改。

If you want to change the environment for your current process this is straight forward:

如果您想更改当前流程的环境,这很简单:

process.env.YOUR_VAR = 'your_value';

回答by Cristian Smocot

Give this a try:

试试这个:

https://www.npmjs.com/package/shelljs

https://www.npmjs.com/package/shelljs

I don't think it is possible for a child process to change the parent's process environment. So I don't really think it is possible to use child_process.

我认为子进程不可能改变父进程的环境。所以我真的不认为可以使用child_process.

Sample code:

示例代码:

var shell = require('shelljs');
shell.exec('export ENV_VARIABLE=ABRACADABRA');