at work I need to make it possible to change the environmet variables at runtime, from an Azure web service, through docker and nginx.
I tried this, this and some similar solutions, but I couln’t get any of them to work.
I also couldn’t find any solution online or any article/thread/post that explained if this is even possible, I only always find the text that vite statically replaces the env variables at build time.
During our CI/CD pipeline vite gets the env variables but our Azure admins want to be able to configure them from Azure, just for the case of it.
Does anyone know if this is possible and or maybe has a solution or some help, please ? 🙂
7
Answers
My Solution is that it schould work with the links from my question. I use this approach and it works, the only thing that needs to be thought of is to use a different variable name/prefix (e.g. "APP_...") so vite doesn't change them at build time. I created a config file wich resolves the variable, for example if the app is in production than it uses the new Variable "APP_.."(which comes injected from nginx/ docker) or use "VITE_..."-variable if "APP_.." is undefined.
You can set the variables in YAML format and update them accordingly as per your requirement.
Below is the sample YAML format which we use as a template:
Check this SO for more insights to understand environment variables hosted in azure web app
It is
not
possible to dynamically injectVite
env variables. But what is possible, is to change thewindow object
variables (assign them on runtime).WARNING!!! DO NOT EXPOSE ANY SENSITIVE VARIABLES THROUGH THE WINDOW OBJECT. YOUR FRONT-END APPLICATION SOURCE IS VISIBLE TO ANYONE USING IT
Steps:
Create your desired env files and place them in
<rootDir>/public
. Let’s call themenv.js
andenv-prod.js
.Inside your
env.js
andenv-prod.js
You want to assign your desired variables usingvar
keyword. Also, you will have to reference these values in your source likewindow.MY_VAR
to be able to use them.Create a script tag inside your
<rootDir>/index.html
like this:<script type="text/javascript" src="./env.js"></script>
.IMPORTANT!!!
type="text/javascript"
is important, because if You specify module,Vite
will include yourenv.js
source inside your minifiedindex.js
file.Vite config (optional):
How to serve the env files on runtime
. Create anode
server which will serve your frontend application. But before serving theenv.js
file, depending on ourprocess.env.ENVIRONMENT
you can now choose which env.js to serve. Let’s say my node server file is stored at<rootDir>/server/server.js
:Serve your application build while running
node ./server/sever.js
command in your terminal.Finally:
my
env.js
containsvar RUNTIME_VAR = 'test'
my
env-prod.js
containsvar RUNTIME_VAR = 'prod'
After I set my
process.env.ENVIRONMENT
toprod
. I get this file served:I came up with a solution and published it as packages to the npm registry.
With this solution, you don’t need to change any code:
It separate the build step out into two build step:
During production it will be statically replaced
import.meta.env
with a placeholder:You can then run the package’s CLI anywhere to replace the placeholders with your environment variables:
Here is the documentation site: https://iendeavor.github.io/import-meta-env/.
Feel free to provide any feedback.
First create .env file in project root,then define a variable in .env
e.g:
VITE_APP_any = 'any'
and then add following line to vite.config.js :
For usage can use following line
import.meta.env.VITE_APP_any
Or
process.env.VITE_APP_any
here is the Dockerfile
I’m building the project in the first stage without any envs,
in the second stage I’m copying the files and then creating the config.json file based on envs that are passed at run time with envstub feature of nginx.
then from the project I called the config.json file and load the envs from there but be careful you can not import it because imports will be resolved at build time instead you have to get it with fetch or axios or any equivalents