如何从导出环境变量的npm脚本中运行bash脚本?

时间:2022-08-04 23:06:47

I have a package.json that has the following script:

我有一个包。具有以下脚本的json:

"scripts": {
  "config": ". ./setup.sh"
},

The setup.sh file prompts the user for an API token,

的设置。sh文件提示用户输入API令牌,

read -p "Enter API Authorization Token: " val
export API_AUTH_TOKEN=$val

and an environment via a PS3 menu. Ex: entering 1 should export DEFAULT_ENV='http://localhost:8000'.

以及通过PS3菜单的环境。例:输入1将导出DEFAULT_ENV='http://localhost:8000'。

And when I run this setup.sh via the terminal(. ./setup.sh), it works great. It's only when I run "npm run config", where it doesn't actually export those values, although it acts like it did. I'm under the impression this is related to this script being a process within the other process, and so, not affecting the global environment. How do I make it so that it does?

当我运行这个设置时。通过终端(. . ./setup.sh),它工作得很好。只有当我运行“npm run config”时,它实际上并没有输出这些值,尽管它的行为是这样的。我的印象是,这与这个脚本在其他过程中是一个过程有关,因此,不影响全局环境。我怎么做呢?

1 个解决方案

#1


3  

This is because export only works in child processes and itself.

这是因为导出只能在子进程和本身中工作。

You can edit your file, adding the line to see it:

您可以编辑您的文件,添加一行来查看:

read -p "Enter API Authorization Token: " val
export API_AUTH_TOKEN=$val
echo $API_AUTH_TOEKEN

In fact it never affects the parent process (like the shell window)

事实上,它不会影响父进程(比如shell窗口)

To affect the global, you should save the variable in files like .bashrc and source .bashrc to make it into effect.

要影响全局,您应该将变量保存在.bashrc和source .bashrc这样的文件中,以使其生效。

#1


3  

This is because export only works in child processes and itself.

这是因为导出只能在子进程和本身中工作。

You can edit your file, adding the line to see it:

您可以编辑您的文件,添加一行来查看:

read -p "Enter API Authorization Token: " val
export API_AUTH_TOKEN=$val
echo $API_AUTH_TOEKEN

In fact it never affects the parent process (like the shell window)

事实上,它不会影响父进程(比如shell窗口)

To affect the global, you should save the variable in files like .bashrc and source .bashrc to make it into effect.

要影响全局,您应该将变量保存在.bashrc和source .bashrc这样的文件中,以使其生效。