Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run bash script from npm script that exports environment variables

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

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

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

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'.

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?

like image 689
wrobbinz Avatar asked Oct 18 '22 08:10

wrobbinz


1 Answers

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)

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

like image 186
SkyAo Avatar answered Oct 20 '22 11:10

SkyAo