Cannot access runtime variables in middlewares
See original GitHub issueRun next info
(available from version 12.0.8 and up)
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021; root:xnu-8019.61.5~1/RELEASE_X86_64
Binaries:
Node: 16.13.0
npm: 8.1.0
Yarn: 1.22.17
pnpm: 6.25.0
Relevant packages:
next: 12.0.8-canary.18
react: 17.0.2
react-dom: 17.0.2
What version of Next.js are you using?
12.0.8-canary.18
What version of Node.js are you using?
16.13.0
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Other platform
Describe the Bug
I cannot access any runtime variables inside _middleware.ts
files.
Expected Behavior
I want to access runtime variables in middlewares.
I couldn’t find any documentation about this and hence can only hope that it is a bug and will be fixed. Currently I can only use process.env.FOO
inside middlewares but that are build time variables, which is not ideal, because I’d prefer to use here server runtime variables.
To Reproduce
Inside _middleware.ts
:
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
import getConfig from 'next/config';
// outside the middleware export
console.log(getConfig());
// inside the middleware export (doesn't matter)
export function middleware(request: NextRequest, _event: NextFetchEvent) {
console.log(getConfig());
return NextResponse.next();
}
...
Always logs undefined
as any runtime config seems to be not accessible in middlewares.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Allow runtime environment variables in middleware #36338
We'd like the ability to use environment variables at runtime in middleware. It is a common application practice to build & bundle an...
Read more >Cannot access req.session variables in Express/NodeJS
In your case, you have assigned req.session.user = user; only in /login request. It will not be available for further requests( /auth )....
Read more >Part 5 - Nuxt 3 Runtime Configuration Variables in ... - YouTube
In this video series about Nuxt 3 Runtime Configuration variables we will deep dive into the many ... Your browser can't play this...
Read more >Use multiple environments in ASP.NET Core | Microsoft Learn
To determine the runtime environment, ASP.NET Core reads from the following environment variables: DOTNET_ENVIRONMENT; ASPNETCORE_ENVIRONMENT ...
Read more >Basic Features: Environment Variables - Next.js
Learn to add and access environment variables in your Next.js application.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
However, I found an ugly workaround. At least as tip for the docs: You should mention in the “Middleware” docs that “next/config” is not working and maybe also why it is not working.
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.