R
Railway•16mo ago
tonyghouse

Unable to read env variables in react vite project (build using Dockerfile)

//.env file VITE_DUMMY_KEY_1=1000 VITE_DUMMY_KEY_2=$PORT VITE_DUMMY_KEY_3=${PORT} VITE_DUMMY_KEY_4=${{PORT}} //checking by logging the keys const k1 = import.meta.env.VITE_DUMMY_KEY_1; console.log("k1::: ",k1); const k2 = import.meta.env.VITE_DUMMY_KEY_2; console.log("k2::: ",k2); const k3 = import.meta.env.VITE_DUMMY_KEY_3; console.log("k3::: ",k3); const k4 = import.meta.env.VITE_DUMMY_KEY_4; console.log("k4::: ",k4);
27 Replies
Percy
Percy•16mo ago
Project ID: N/A
tonyghouse
tonyghouseOP•16mo ago
N/A
Brody
Brody•16mo ago
please send your dockerfile
tonyghouse
tonyghouseOP•16mo ago
FROM node:18.16-alpine


WORKDIR /app

COPY . /app

RUN rm -rf node_modules

RUN npm install -g typescript

RUN npm install --legacy-peer-deps

RUN npm run build

COPY . .

EXPOSE 8080

CMD ["npm", "start"]
FROM node:18.16-alpine


WORKDIR /app

COPY . /app

RUN rm -rf node_modules

RUN npm install -g typescript

RUN npm install --legacy-peer-deps

RUN npm run build

COPY . .

EXPOSE 8080

CMD ["npm", "start"]
Brody
Brody•16mo ago
interesting enclose your code with triple backticks please can you share your repo please?
tonyghouse
tonyghouseOP•16mo ago
GitHub
GitHub - tonyghouse/memoirist-ui
Contribute to tonyghouse/memoirist-ui development by creating an account on GitHub.
Brody
Brody•16mo ago
any reason you aren't building with nixpacks?
tonyghouse
tonyghouseOP•16mo ago
using nixpacks but due to some dependencies i have to use --legecy-deps commnad so changed deployment to docker but why variables are not accessible in env
Brody
Brody•16mo ago
you should really fix the dependency conflicts
tonyghouse
tonyghouseOP•16mo ago
used docker for java project there i was able to access variables from railway so there is no way to access variables in docker?
Brody
Brody•16mo ago
yes of course there is but that's not the biggest issue here have you set the variables in your service and can you show me a screenshot of the service variables
tonyghouse
tonyghouseOP•16mo ago
Brody
Brody•16mo ago
okay now fix your dependency conflicts and move back to nixpacks let me know when that's done
tonyghouse
tonyghouseOP•16mo ago
used nix packs but still variables are not accesible its weird build is successful app is deployed but variables are not accessible
Brody
Brody•16mo ago
you've fixed the dependency conflicts?
tonyghouse
tonyghouseOP•16mo ago
yes wait its accessible now able to access variables seems deployment takes time to go live
Brody
Brody•16mo ago
have you removed or renamed the dockerfile
tonyghouse
tonyghouseOP•16mo ago
deleted the docker file just nixpacks is used to build
Brody
Brody•16mo ago
perfect
tonyghouse
tonyghouseOP•16mo ago
but why env not worked during docker still bugs me
Brody
Brody•16mo ago
because you didn't define the variables in the dockerfile, nixpacks does this for you automatically, and that's why I had you move back to nixpacks
tonyghouse
tonyghouseOP•16mo ago
is there any documentation or tutorial where we can access variables from railway and set it in during docker file. helpful if you can share if you have
Brody
Brody•16mo ago
you aren't using a dockerfile anymore?
tonyghouse
tonyghouseOP•16mo ago
yes but in future if i need coz some times i use old libs for the projects there i cant resolve have to go with --legecy-deps command
Brody
Brody•16mo ago
okay well it's simple enough let's say you add a service variable VITE_TEST = HELLO you would just have to place ARG VITE_TEST in your dockerfile under the FROM line no no, there is always a better way to resolve peer deps
tonyghouse
tonyghouseOP•16mo ago
thanks brody
Brody
Brody•16mo ago
no problem 🙂
Want results from more Discord servers?
Add your server