-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Env variables are not accessed by vite in PROD #3004
Comments
|
@Conduitry Yes, Dockerfile is setting them at app runtime with |
.env
variable.ts
NOTE: import.meta.env don't have api.ts
after the build My docker image already has all the env variable inside the system |
What happens if you change |
There should be some improvement on that front with Vite 2.7: vitejs/vite#5404 |
I don't think
// env.js
window.__MY_ENV__ = "something" // your-code.js
console.log(__MY_ENV__) Keep in mind that you need to shim it in dev and SSR if needed, possible with
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@benmccann Tried upgrading to Reproduction repo available here. |
Sort of duplicate: #3030 |
Please see the environment variable FAQ: https://kit.svelte.dev/faq#env-vars To use at runtime you'll need to instantiate yourself (see svelte.dev's hooks.js for an example). You can then pass to your templates using |
This comment was marked as off-topic.
This comment was marked as off-topic.
This problem is still here and quite infuriating. |
This is an incredibly ugly hack to get this working, it converts process.env variables prefixed with VITE_ to an .env file before before runtime. docker build file
package.json (I have to use preview instead of dev, as otherwise webworkers break on ff, ive been having fun :/) "scripts": {
"build": "node ./.patch/env_bind.js && vite build && rm .env",
"preview": "node ./.patch/env_bind.js && vite build && rm .env && vite preview --host"
}, .patch/env_bind.js // Vite imports environment variables through import.meta.env. import.meta.env does not import docker variables like process.env does. This file patches this.
import fs from "fs";
const vite_env_variables = Object.entries(process.env).filter(variable => variable[0].startsWith("VITE_"));
const vite_env_variable_string = vite_env_variables.map(variable => variable[0] + "=" + variable[1]).join("\n");
fs.writeFileSync(".env", vite_env_variable_string, {encoding: "utf-8"}); I don't recommend using this, but in my case I dont have another choice until import.meta.env gets all the env variables |
Hey, any update on this? |
I solved this by building the .env from a shell script, using a pre-populated .env with docker-entry.sh:
|
https://stackoverflow.com/a/77490465/17756110 If anyone is still looking for the solution to this problem. |
Describe the bug
I am using the docker image for the production build. I have set my environment variables in docker and they are accessible by using
env
. But when I build the app these are not accessed by Vite.Please help me here!
Reproduction
Create Svelte kit app by started.
Logs
No response
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response
The text was updated successfully, but these errors were encountered: