2 months ago
Not receiving any environment variables at runtime on the build logs.
I've set variables like NEXT_PUBLIC_API_KEY in the frontend service and API_KEY in the backend service and more into the service's "Variables" tab, but my application logs show that process.env.NEXT_PUBLIC_API_KEY get a 401 error when in local with the exact same configs it works perfectly.
The build log shows a successful build. I have two services running, each of them with their individual docker container. BE and FE, which should communicate with each other.
Can you please investigate why the variables are not being injected into my container?
24 Replies
2 months ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
2 months ago
For the record: I have committed no mistypings, runs perfectly locally, but variables are not pulled in for some reason.
2 months ago
When I do 'railway variables' via CLI i get the variables correctly..
2 months ago
Hey there! We had something similar happen recently.
Since setting the variables have you saved them?
On that, have you redeployed the service after saving the new variables?
noahd
Hey there! We had something similar happen recently. Since setting the variables have you saved them? On that, have you redeployed the service after saving the new variables?
2 months ago
Yes to both questions. That's why I am quite confused.
felixggj
Yes to both questions. That's why I am quite confused.
2 months ago
Always love to clear up the given simple things!
So in your script if you log process.env.NEXT_PUBLIC_API_KEY you are getting a 401 error?
Can you try printing the value to console of that variable?
2 months ago
Yes, I get 'undefined'.
noahd
Always love to clear up the given simple things! So in your script if you log process.env.NEXT_PUBLIC_API_KEY you are getting a 401 error? Can you try printing the value to console of that variable?
2 months ago
Yes, I get 'undefined'.
felixggj
Yes, I get 'undefined'.
2 months ago
For all my variables, not just this one.
2 months ago
Okay great that clears things up more!
For a bit more cursory info, are you using nixpacks/railpacks or a Dockerfile?
2 months ago
Dockerfile in a monorepo, one for backend and one for frontend, two different railway services:
be :
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app ./app
CMD ["sh", "-c", "python -m uvicorn app.api.main:app --host 0.0.0.0 --port ${PORT:-8080}"]fe:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["sh", "-c", "npm start -- --port ${PORT:-8080} --hostname 0.0.0.0"]
The builds and deployments are successful, I set it up with git root directories for each service.
noahd
Thank you so much! Doing some poking around now
2 months ago
okay thanks a lot, let me know!
felixggj
okay thanks a lot, let me know!
2 months ago
https://nextjs.org/docs/14/app/building-your-application/configuring/environment-variables
so I did find some things about env variables with nextjs.
Did you follow this where you declare in next conf the env variables it would be referencing? That could be it but I'm not entirely sure.
noahd
https://nextjs.org/docs/14/app/building-your-application/configuring/environment-variablesso I did find some things about env variables with nextjs. Did you follow this where you declare in next conf the env variables it would be referencing? That could be it but I'm not entirely sure.
2 months ago
just tried implementing this like this:
/** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone",
env: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
NEXT_PUBLIC_API_KEY: process.env.NEXT_PUBLIC_API_KEY,
},
};
module.exports = nextConfig;
in a next.config.js file, re-deployed, but still getting that 401 issue :(
felixggj
just tried implementing this like this:/** @type {import('next').NextConfig} */ const nextConfig = { output: "standalone", env: { NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL, NEXT_PUBLIC_API_KEY: process.env.NEXT_PUBLIC_API_KEY, }, }; module.exports = nextConfig;in a next.config.js file, re-deployed, but still getting that 401 issue :(
2 months ago
So 401 is unauthorized.
I'm a bit confused as to why the env variables are causing a 401 (other then an undefined value mismatching provided value).
If we're just talking about the value of the NEXT_PUBLIC_API_URL/KEY is that being propagated? As in, printing to console is the value undefined?
noahd
So 401 is unauthorized. I'm a bit confused as to why the env variables are causing a 401 (other then an undefined value mismatching provided value).If we're just talking about the value of the NEXT_PUBLIC_API_URL/KEY is that being propagated? As in, printing to console is the value undefined?
2 months ago
yes exactly, in console it says exactly 'undefined', printed. It's a 401 because it's an api that requires a key to call. I am calling it in my frontend and it's returning 401 error. headers are empty for x-api-key .
In local it works as expected, no issues.
2 months ago
Okay wonderful thanks for clarifying!
The other thing I can think is that during the build cycle this isnt properly passing the variables in.
https://docs.railway.com/guides/dockerfiles#using-variables-at-build-time
Looks like you need to declare them in your Dockerfile like:ARG NEXT_PUBLIC_API_KEY etc.
2 months ago
are you using docker by chance? I had to use ARG to copy over my env variables to the container.
2 months ago
I just deployed a custom docker this morning. it was a pita. https://github.com/profullstack/qryptchat-web/blob/master/Dockerfile
2 months ago
Wow that is complex! Does look like ARG will fix that then. Wish next was easier to deal with
2 months ago
It worked!! thanks a lot
felixggj
It worked!! thanks a lot
2 months ago
Happy I could help!
Noted for the future too.
Status changed to Solved noahd • 2 months ago


