Pages, Sveltekit and Environment variables
Hello,
I'm trying to use Cloudflare pages to deloy our web application.
However I'm getting errors in the build for each env var, public or private.
I've tried adding these in both the
wrangler.tom
and in the settings > environment variables
tab. Neither has any effect on the build errors.
My build command, from the package.json
file is "build": "npx svelte-kit sync && npx svelte-check && vite build",
Any help is appreciated.
----
It seems many others are having the same problem and no one has found a solution.6 Replies
have you made sure that you have followed the rules of naming conventions of Vite, so that Vite can see them ?
https://vitejs.dev/guide/env-and-mode
Sveltekit uses a different naming convention. It uses
PRIVATE
and PUBLIC
prefixes - https://kit.svelte.dev/docs/modules#$env-dynamic-private. And this is what I'm using. It works when I run the same build command locally.SvelteKit docs
Modules • SvelteKit documentation
Ok, I tried prefixing with
VITE_
and still get the same error.@Magick there is some other issue, I dontknow much about svelteKit, so sorry couldn't help ya
I've tried the following:
- process.env.VARIABLE
- import.meta.env.VARIABLE
Neither of these are working either.
I was struggling on this yesterday too 😅 . You'll need to add the Platform interface like below, but noting I was trying to use D1. After this i was able to use
platform.env.DB
For regular env variables i to still add them to wrangler.toml and .env to get it to work with the $public/$private like you were mentioning