Svektekit public env during build
melchione
PROOP

10 months ago

On my sveltekit service I have some public environment variables :

import { PUBLIC_FAST_API_URL, PUBLIC_FAST_API_WS_URL } from '$env/static/public';

But during the build I got strange error :

src/lib/chatLogic.svelte.js (2:9): "PUBLIC_FAST_API_URL" is not exported by "virtual:env/static/public", imported by "src/lib/chatLogic.svelte.js".

I need to get some env variable (public) in my front. Do I miss something?

$10 Bounty

5 Replies

10 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 10 months ago


lofimit
HOBBY

10 months ago

Could it be that those PUBLIC_ env vars aren’t set during the build? SvelteKit’s $env/static/public needs those vars to exist before you build, otherwise it breaks.

As far as I can remember Railway does inject env vars during build, so I think you forgot to add PUBLIC_FAST_API_URL and PUBLIC_FAST_API_WS_URL in your Railway project’s build environment variables.

If you’re not sure or can’t set those for build, you can switch your import to $env/dynamic/public so it grabs the vars at runtime instead and won’t break during build.


melchione
PROOP

10 months ago

Thanks for your answer, but I already set those env variables in my railway service, I also tried with dynamic/public but I get the same error.

error during build:

src/lib/chatLogic.svelte.js (2:9): "PUBLIC_FAST_API_URL" is not exported by "virtual:env/dynamic/public", imported by "src/lib/chatLogic.svelte.js".

file: /app/src/lib/chatLogic.svelte.js:2:9

2: import { PUBLIC_FAST_API_URL, PUBLIC_FAST_API_WS_URL } from '$env/dynamic/public';

:(


melchione

Thanks for your answer, but I already set those env variables in my railway service, I also tried with dynamic/public but I get the same error.error during build:src/lib/chatLogic.svelte.js (2:9): "PUBLIC_FAST_API_URL" is not exported by "virtual:env/dynamic/public", imported by "src/lib/chatLogic.svelte.js".file: /app/src/lib/chatLogic.svelte.js:2:92: import { PUBLIC_FAST_API_URL, PUBLIC_FAST_API_WS_URL } from '$env/dynamic/public';:(

lofimit
HOBBY

10 months ago

Instead of:
import { PUBLIC_FAST_API_URL } from '$env/dynamic/public';

Could you try:
import * as publicEnv from '$env/dynamic/public';

const url = publicEnv.PUBLIC_FAST_API_URL;


lofimit

Instead of:import { PUBLIC_FAST_API_URL } from '$env/dynamic/public';Could you try:import * as publicEnv from '$env/dynamic/public';const url = publicEnv.PUBLIC_FAST_API_URL;

melchione
PROOP

10 months ago

Thank you for your answer, I tried it, the build pass but the publicEnv.PUBLIC_FAST_API_URL is empty. I also tried with $env/static/public, still empty. :s


melchione

Thank you for your answer, I tried it, the build pass but the publicEnv.PUBLIC_FAST_API_URL is empty. I also tried with $env/static/public, still empty. :s

lofimit
HOBBY

10 months ago

Damn... Well last hope I have, sorry but my knowledge is a bit limited haha
In your package.json, modify your build script to explicitly pass the env vars:

json{

"scripts": {

"build": "PUBLIC_FAST_API_URL=$PUBLIC_FAST_API_URL PUBLIC_FAST_API_WS_URL=$PUBLIC_FAST_API_WS_URL vite build"

}

}
I still think that most likely Railway isn't making these variables available during the build step.
If changing the package.json to explicitly pass the env vars doesn't work, then make sure your PUBLIC_FAST_API_URL and PUBLIC_FAST_API_WS_URL are set in the Variables tab of your Railway service and there are no typos in the variable names.

And last resort is probably creating a .env file in your project root.


Loading...