-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Environment variables in Svelte templates don't work with npm run build
#720
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
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Avoiding dot notation with environment variables fixed the issue for me. |
This is a tricky issue and I'm not immediately sure what the best fix is, but a workaround in the meantime would be to put the env var references in a module (rather than a component) and import them into your components: export const API_URL = import.meta.env.VITE_API_URL; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
npm run build
Small but interesting data point: I ran into this issue when adding a |
This comment has been minimized.
This comment has been minimized.
AFAIK nothing new, but the suggested workaround works like a charm. |
and to be honest I actually prefer the workaround because it lets you sanitize/transform/fallback the environment variables values that Eg. I have some env vars that behave like flags ( |
Investigated this today, #1396 (comment) was right. IMO there are some ways we can fix this (easy to hard):
I'll probably try no2 first, before no1 |
I've sent a PR in Svelte implementing solution no1: sveltejs/svelte#6744 |
From sveltejs/svelte#6744, it was closed in favour of a new |
This sounds like the best solution to me. Any idea what tooling would expect it to be statically replaced? |
Actually, answering my own question I think that would kill Rollup / Vite's ability to do dead code elimination on things like client-side vs browser-side code, so probably we would not want to do it that way on the client. But it still seems okay on the server. Perhaps we don't want client and server acting differently though |
Created my own repro: arxpoetica/sveltekit-6qwj7g
Originally was here #2583, but @benmccann appropriately closed the duplicate. |
For reference, here's the code that's being generated in @arxpoetica's example
It's the |
Leaving this open for now. It's only partially solved. We will need to update |
From my perspective, it's not okay on the server because there're a lot of examples where environment gets changed for server-code and it should reflect the change. E.g. if I use a hundred different APIs, I don't want to rebuild the app every time my credentials are changed. One size does not fit all and it makes a lot of sense to have different behaviour for a client and a server codelines. |
First of all, thank you for the great work on SvelteKit.
I migrated my blog today without too many problems, the docs, the migration guide and examples were a great help.
I just stumbled upon this problem.
Describe the bug
It seems like we can't use environment variables (e.g.
import.meta.env.MODE
) in a (production) build while there are also styles defined in the same route/component.It does however work when I start the server and open the app with
npm run dev
I also tried to follow the vite docs, but sadly with the same outcome.
Logs
To Reproduce
A reproduction can be found at: https://github.com/timdeschryver/sk-environment-variables/blob/main/src/routes/index.svelte
Steps to reproduce:
The code:
Expected behavior
I expect the environment variable to be replaced.
Information about your SvelteKit Installation:
npx envinfo --system --npmPackages svelte,@sveltejs/kit --binaries --browsers
I get the error with all adapters, node, static, and vercel.
Severity
I have a workaround (I added the variables to a GET request), but I still wanted to log this issue because it might help others.
Additional context
/
The text was updated successfully, but these errors were encountered: