R
Railwayβ€’15mo 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β€’15mo ago
Project ID: N/A
tonyghouse
tonyghouseβ€’15mo ago
N/A
Brody
Brodyβ€’15mo ago
please send your dockerfile
tonyghouse
tonyghouseβ€’15mo 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β€’15mo ago
interesting enclose your code with triple backticks please can you share your repo please?
tonyghouse
tonyghouseβ€’15mo ago
GitHub
GitHub - tonyghouse/memoirist-ui
Contribute to tonyghouse/memoirist-ui development by creating an account on GitHub.
Brody
Brodyβ€’15mo ago
any reason you aren't building with nixpacks?
tonyghouse
tonyghouseβ€’15mo 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β€’15mo ago
you should really fix the dependency conflicts
tonyghouse
tonyghouseβ€’15mo 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β€’15mo 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
tonyghouseβ€’15mo ago
Brody
Brodyβ€’15mo ago
okay now fix your dependency conflicts and move back to nixpacks let me know when that's done
tonyghouse
tonyghouseβ€’15mo 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β€’15mo ago
you've fixed the dependency conflicts?
tonyghouse
tonyghouseβ€’15mo ago
yes wait its accessible now able to access variables seems deployment takes time to go live
Brody
Brodyβ€’15mo ago
have you removed or renamed the dockerfile
tonyghouse
tonyghouseβ€’15mo ago
deleted the docker file just nixpacks is used to build
Brody
Brodyβ€’15mo ago
perfect
tonyghouse
tonyghouseβ€’15mo ago
but why env not worked during docker still bugs me
Brody
Brodyβ€’15mo 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
tonyghouseβ€’15mo 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β€’15mo ago
you aren't using a dockerfile anymore?
tonyghouse
tonyghouseβ€’15mo 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β€’15mo 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
tonyghouseβ€’15mo ago
thanks brody
Brody
Brodyβ€’15mo ago
no problem πŸ™‚
Want results from more Discord servers?
Add your server