Can't access runtime environment variables in middleware
Unanswered
Broad-snouted Caiman posted this in #help-forum
Broad-snouted CaimanOP
Hello,
Should Next.js be able to access node environment variables at runtime in the middleware? I know that variables used in next config are baked during build time. But what about middleware?
Should Next.js be able to access node environment variables at runtime in the middleware? I know that variables used in next config are baked during build time. But what about middleware?
26 Replies
Hello @Broad-snouted Caiman π
It could perhaps help you : https://nextjs.org/docs/app/building-your-application/deploying#middleware
It could perhaps help you : https://nextjs.org/docs/app/building-your-application/deploying#middleware
@ItetsuLaTable Hello <@749252310471016510> π
It could perhaps help you : https://nextjs.org/docs/app/building-your-application/deploying#middleware
Broad-snouted CaimanOP
It doesn't, unfortunately. I understand that middleware uses edge but does that mean it can't access process.env? I've read middleware and runtime docs several times already but I can't find a concrete answer.
Can you show what you've try please ?
Broad-snouted CaimanOP
middleware.js
App is built with Docker and environment variable is injected during rollout in Kubernetes. I've checked the Kubernetes pod and I see environment variable is availabe in node. But in middleware it is undefined.
import { NextResponse } from 'next/server';
export function middleware(request) {
const backendUrl = process.env.BACKEND_URL;
console.log('backendUrl', backendUrl);
if (!backendUrl) {
return NextResponse.next();
}
const url = request.nextUrl.clone();
if (url.pathname.startsWith('/api/v1/')) {
const newUrl = `${backendUrl}${url.pathname}${url.search}`;
return NextResponse.rewrite(new URL(newUrl));
}
return NextResponse.next();
}App is built with Docker and environment variable is injected during rollout in Kubernetes. I've checked the Kubernetes pod and I see environment variable is availabe in node. But in middleware it is undefined.
On my side i'm doing this
export function middleware(request: NextRequest) {
const response = NextResponse.next();
if (!VARNISH && process.env.NODE_ENV !== 'production') // SOME CODE ... and its workingAre you sure you're passing correctly the env with k8s ?
The way you're calling the env variable is good. The issue may be from somewhere else
Broad-snouted CaimanOP
Yeap, I'm passing correctly, I can exec in pod that is running my next app and I can see env variable with value in node process.env
I'll bump next to latest (currently 14.1.4), perhaps that will help :/
Are you passing env through k8s or you have multiple env file ?
I dunno much how it works (i've done it)
BUT I have one env file per server
BUT I have one env file per server
Broad-snouted CaimanOP
We have environment section in actual k8s yaml files, it's somewhat a standardized way to pass env variables to apps in our company k8s configs
I dont think so that is the way you use it on middleware (because that's the good way to do it)
I am not an expert with k8s I cannot help you more sorry π¦
but i guess you need an env file to make it works
Broad-snouted CaimanOP
You've helped enough, I can at least debug further knowing that it should work and it works for you π
I have not understand what you're doing with k8s if you're passing env in cmd line or with env file like the screen i've sent ?
One more question are you using app dir or pages dir ?
Broad-snouted CaimanOP
app dir
Passing in yaml file, stripped down version looks like:
# Number of pods that will be running
replicaCount: 1
image:
repository: ...
tag: '...'
containerPort: 3000
probes:
liveness:
url: /api/health/live
readiness:
url: /api/health/ready
networkPolicy: AllowExternal
service:
type: ClusterIP
port: 8080
resources:
requests:
cpu: 256m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
environment:
BACKEND_URL: 'some-urlI don't know how it works further down the line but whatever is defined in environment is injected during rollout and is available in process.env in runtime. However, next.js does not see it.
Uhm perhaps there is the issue π¦
Can you try to create an .env file with your backend url and run it locally to see what happens ?
Can you try to create an .env file with your backend url and run it locally to see what happens ?
Broad-snouted CaimanOP
Locally everything works fine, with env file or just exporting value with run command
So we know that issue is not coming from you code!
Good luck for debbuging it
I cant help more I hope you'll find a solution
I cant help more I hope you'll find a solutionBroad-snouted CaimanOP
Thank you π