R
Railway10mo ago
Thijs

Environment variables not being injected in NodeJS app

I have a payloadCMS build command that cannot finish with railway even though I added all my .env variables as service variables. For instance I have set NODE_ENV=production, but inside the application there is still NODE_ENV=develop.
No description
No description
Solution:
you need to specify the needed variables via the ARG keyword https://docs.railway.app/guides/dockerfiles#using-variables-at-build-time...
Jump to solution
10 Replies
Percy
Percy10mo ago
Project ID: d426ba88-e4ae-4a98-8b5a-b473f6902b16
Thijs
Thijs10mo ago
d426ba88-e4ae-4a98-8b5a-b473f6902b16
Brody
Brody10mo ago
are you building with a dockerfile?
Thijs
Thijs10mo ago
Yes I use the default docker file
FROM node:18-alpine as base

FROM base as builder

WORKDIR /home/node
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production

WORKDIR /home/node
COPY package*.json ./

RUN yarn install --production
COPY --from=builder /home/node/dist ./dist
COPY --from=builder /home/node/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
FROM node:18-alpine as base

FROM base as builder

WORKDIR /home/node
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production

WORKDIR /home/node
COPY package*.json ./

RUN yarn install --production
COPY --from=builder /home/node/dist ./dist
COPY --from=builder /home/node/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
Solution
Brody
Brody10mo ago
you need to specify the needed variables via the ARG keyword https://docs.railway.app/guides/dockerfiles#using-variables-at-build-time
Thijs
Thijs10mo ago
Can you give me an example for NODE_ENV?
Brody
Brody10mo ago
ARG NODE_ENV
Thijs
Thijs10mo ago
So all env keys I need to pass to access through ARG.
ARG NODE_ENV

ENV NODE_ENV=$NODE_ENV
ARG NODE_ENV

ENV NODE_ENV=$NODE_ENV
?
Brody
Brody10mo ago
please have a look at the doc section I linked, ENV is not needed here
Thijs
Thijs10mo ago
Thanks! I'm getting somewhere now I followed a different tutorial which is out of date, because it doesn't use ARG
Want results from more Discord servers?
Add your server