Skip to content

How to use env variables in the middleware? #49

@glung

Description

@glung

The package claims to work with the middleware.

Though, in my experience (see below), I could not get it to work. Considering the middleware is an Edge runtime, that probably makes sense. Is there some other mechanism to support this use case?


SERVER_ENV=server NEXT_PUBLIC_ENV=client_server next dev
import { env } from "next-runtime-env";
import { NextRequest, NextResponse } from "next/server";

export async function middleware(req: NextRequest) {
  console.log("SERVER_ENV", env("SERVER_ENV));
  console.log("NEXT_PUBLIC_ENV", env("NEXT_PUBLIC_ENV"));

[...]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions