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.
Error: Module '"$env/static/public"' has no exported member 'PUBLIC_SUPABASE_URL'.
Error: Module '"$env/static/public"' has no exported member 'PUBLIC_SUPABASE_URL'.
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
CondenseDrop
CondenseDrop•6mo ago
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
vitejs
Env Variables and Modes
Next Generation Frontend Tooling
Magick
MagickOP•6mo ago
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
Magick
MagickOP•6mo ago
Ok, I tried prefixing with VITE_ and still get the same error.
CondenseDrop
CondenseDrop•6mo ago
@Magick there is some other issue, I dontknow much about svelteKit, so sorry couldn't help ya
Magick
MagickOP•6mo ago
I've tried the following: - process.env.VARIABLE - import.meta.env.VARIABLE Neither of these are working either.
zsarver
zsarver•6mo ago
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
interface Platform {
env: {
DB: D1Database;
};
context: {
waitUntil(promise: Promise<any>): void;
};
caches: CacheStorage & { default: Cache }
}
interface Platform {
env: {
DB: D1Database;
};
context: {
waitUntil(promise: Promise<any>): void;
};
caches: CacheStorage & { default: Cache }
}
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
Want results from more Discord servers?
Add your server