process.env is an empty object in _middleware.ts
See original GitHub issueWhat version of Next.js are you using?
12.0.1
What version of Node.js are you using?
14.18.1
What browser are you using?
Brave
What operating system are you using?
macOS 11.5.2
How are you deploying your application?
npm run dev and Vercel
Describe the Bug
Accessing process.env
returns an empty object {}
in pages/_middleware.ts
.
This happens locally when running npm run dev
and also when app is deployed to Vercel.
Expected Behavior
Following the Vercel edge functions docs, environment variables should be available via process.env
.
https://vercel.com/docs/concepts/functions/edge-functions#environment
To Reproduce
Create a middleware file pages/_middleware.ts
:
import { NextResponse } from 'next/server';
export function middleware() {
console.log(process.env);
return NextResponse.next();
}
Start the app, visit any page of the app and inspect console output.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:8 (3 by maintainers)
Top Results From Across the Web
node.js - Why is process.env returning an empty object, while ...
The issue with process.env variable being empty in browser is because browser doesn't have real access to the process of the node.js ....
Read more >Top 10 Most Common Node.js Developer Mistakes - Toptal
Mistake #1: Blocking the event loop. JavaScript in Node.js (just like in the browser) provides a single threaded environment. This means that no...
Read more >next.config.js: Environment Variables
Trying to destructure process.env variables won't work due to the nature of webpack DefinePlugin. For example, the following line: return ...
Read more >envalid - npm
Envalid. Envalid is a small library for validating and accessing environment variables in Node.js (v8.12 or later) programs, aiming to:.
Read more >The env property - Nuxt
Share environment variables between client and server. ... Type: Object ... Make sure to read about process.env and process.env == {} below for...
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
In next.config.js:
But then I realise to change vars to API_BASE and API_AUTH. Calling
process.env.API_AUTH
returns it’s value.I believe it’s documented here https://nextjs.org/docs/api-reference/next.config.js/environment-variables