在node.js中设置一个环境变量,然后在终端中使用它? [英] set an environmental variable in node.js and then use it in terminal?

查看:147
本文介绍了在node.js中设置一个环境变量,然后在终端中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想在使用node.js执行js文件时添加环境变量。




process.env ['VARIABLE'] ='value';



我正在使用终端运行一个模块的js文件,我可以设置变量,然后使用它在js文件执行期间,但是我想使用process.env设置变量,然后当执行结束时,我想在终端中使用它,或者在另一个js进程中使用它。 p>

我知道可以使用child_process.exec,并使用SET(Windows)或EXPORT(Mac& Linux),但只是问如何可能可能的,或使用哪个设计或过程来添加它,只需使用process.env。



提前感谢,人们。

解决方案

unix权限模型将不允许子进程(您的node.js应用程序)更改其父进程的环境(shell运行在哟内) ur终端)。同样适用于当前工作目录,有效的uid,有效的gid和其他几个过程参数。 AFAIK没有直接的方法来做你所要求的。您可以执行打印命令将其设置为stdout,以便用户可以轻松地将该shell命令复制/粘贴到终端中,但最好的办法是解释您在单独问题中尝试解决的更广泛的问题,并让人们告诉你可行的方法来做到这一点,而不是试图改变父进程的环境。



一个可能的解决方法是简单的,从终端运行节点程序如下所示:

  export SOME_ENV_VAR =$(node app.js)

,并且具有 app.js 只需通过进程打印所需的值。 stdout.write



第二个黑客将是以下这些行的封装shell脚本:



app.sh

 #!/ bin / bash 
echo app。 sh运行SOME_ENV_VAR = $ {SOME_ENV_VAR}
echoapp.sh running app.js
export SOME_ENV_VAR =$(node app.js)
exec / bin / bash

app.js

  console.log(Some Value at+ Date()); 

在终端的交互式shell中运行此功能

  echo $ SOME_ENV_VAR 

exec ./app.sh
运行SOME_ENV_VAR =
app.sh运行应用程序的app.sh。 js

echo $ SOME_ENV_VAR
2014年3月27日的某些值2014年08:13:01 GMT-0600(MDT)

也许这些会给你一些想法。


well I'd like to add an environmental varible during the execution of a js file using node.js.

Something like: process.env['VARIABLE'] = 'value';

I'm using the terminal to run the js file using a module, I can set the variable and then use it in during the js file execution, but I'd like to set the variable using "process.env", and then when the execution ends, I'd like to use it in the terminal, or in another js process as well.

I know that it's possible using the child_process.exec, and use SET (Windows) or EXPORT (Mac & Linux), but just asking how it can be possible it can be possible, or using which design or process to add it, just using "process.env".

Thanks in advance, folks.

解决方案

The unix permissions model will not allow a child process (your node.js app) to change the environment of its parent process (the shell running inside your terminal). Same applies to current working directory, effective uid, effective gid, and several other per-process parameters. AFAIK there's no direct way to do what you are asking. You could do things like print the command to set it to stdout so the user can easily copy/paste that shell command into their terminal, but your best bet is to explain the broader problem you are trying to solve in a separate question and let folks tell you viable ways to get that done as opposed to trying to change the parent process's environment.

One possible workaround would be something is simple as running your node program from the terminal like this:

export SOME_ENV_VAR="$(node app.js)"

and have app.js just print the desired value via process.stdout.write.

Second hack would be a wrapper shell script along these lines:

app.sh

#!/bin/bash
echo app.sh running with SOME_ENV_VAR=${SOME_ENV_VAR}
echo "app.sh running app.js"
export SOME_ENV_VAR="$(node app.js)"
exec /bin/bash

app.js

console.log("Some Value at " + Date());

Running this in an interactive shell in your terminal

echo $SOME_ENV_VAR

exec ./app.sh
app.sh running with SOME_ENV_VAR=
app.sh running app.js

echo $SOME_ENV_VAR 
Some Value at Thu Mar 27 2014 08:13:01 GMT-0600 (MDT)

Maybe these will give you some ideas to work with.

这篇关于在node.js中设置一个环境变量,然后在终端中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆