R
Railway•5mo ago
insane_banda

Vite Env variables is undefined for react project

I am using Vite + React for my FE. On my local I am using .env to add VITE_API_URL that can be accessed like import.meta.env.VITE_API_URL so that I can make API request but upon viewing in the network tab I can see that it is undefined. I added the variable in railway from the Variables tab. Also I am using serve to serve the build files. Below are the scripts in my package.json
"scripts": {
"build": "vite build",
"dev": "vite",
"preview": "vite preview",
"start": "serve dist -s -n -L -p $PORT"
}
"scripts": {
"build": "vite build",
"dev": "vite",
"preview": "vite preview",
"start": "serve dist -s -n -L -p $PORT"
}
14 Replies
Percy
Percy•5mo ago
Project ID: ed9d81eb-a192-462c-8cf6-bf7778244e57
Percy
Percy•5mo ago
Project ID: ed9d81eb-a192-462c-8cf6-bf7778244e57
insane_banda
insane_banda•5mo ago
ed9d81eb-a192-462c-8cf6-bf7778244e57
Fragly
Fragly•5mo ago
so you're uploading a .env file to railway?
insane_banda
insane_banda•5mo ago
no not uploading but using the variable section from railway dashboard
Fragly
Fragly•5mo ago
oh okay I'm personally not sure what's wrong here since I don't use Vite or React so let's wait for someone who knows to step in
insane_banda
insane_banda•5mo ago
Okay
insane_banda
insane_banda•5mo ago
I have added my variable here then why is it undefined?
No description
No description
insane_banda
insane_banda•5mo ago
@Brody could you please help?
Fragly
Fragly•5mo ago
( #🛂|readme #5 )
maddsua
maddsua•5mo ago
Could it be possible that the variable is not accessible during build stage thonk
insane_banda
insane_banda•5mo ago
Noted Anyways I was able to solve this issue. I was using docker but I saw other post where it was metioned to use nixpacks instead of docker so I removed the dockerfile and added dependency from VITE + React template and it worked.
Fragly
Fragly•5mo ago
I'm glad to hear that 🔥 Sorry that I couldn't be of more help glad you figured it out :)
insane_banda
insane_banda•5mo ago
No issues, thanks anyways