question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

process.env is an empty object in _middleware.ts

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Defitecommented, Dec 7, 2021

Hey @Defite where are you defining your env vars?

In next.config.js:

module.exports = {
  publicRuntimeConfig: {
    NEXT_PUBLIC_API_BASE: process.env.API_BASE,
    NEXT_PUBLIC_API_AUTH: process.env.API_AUTH,
  },
}

But then I realise to change vars to API_BASE and API_AUTH. Calling process.env.API_AUTH returns it’s value.

2reactions
hugefunwoocommented, Oct 30, 2021

I believe it’s documented here https://nextjs.org/docs/api-reference/next.config.js/environment-variables

Next.js will replace process.env.customKey with ‘my-value’ at build time. Trying to destructure process.env variables won’t work due to the nature of webpack DefinePlugin.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found