Skip to content

[BUG]: Next.js config plugin crashes middleware (edge runtime) #412

@Sgouws

Description

@Sgouws

varlock version

0.5.0, @varlock/nextjs-integration 0.2.3

Problem

Using varlockNextConfigPlugin() crashes any Next.js app that has middleware. Every request returns 500:

⨯ Error [TypeError]: Native module not found: varlock/env
    at <unknown> (.next/server/edge-runtime-webpack.js:3:11)

Removing the plugin and using only the @next/env override works fine.

Root cause

The plugin injects patch-next-runtime.js into both the Node.js server runtime and the edge runtime (edge-runtime-webpack.js). That patch file does require('varlock/env'), which works in Node.js but fails in the edge sandbox since it only allows native modules.

The _edgeRuntime parameter in injectVarlockInitIntoWebpackRuntime() is accepted but never used — edge gets the exact same patch as Node.js.

Reproduction

  1. Next.js 15 project with a middleware.ts (Clerk, Auth.js, or even a bare NextResponse.next())
  2. Add the @next/env override and varlockNextConfigPlugin() per the docs
  3. Start the dev server, navigate to any page, get a 500

Workaround

Use the @next/env override without the config plugin. Loses leak detection and ENV import support, but env loading and validation work fine.

System Info

Next.js: 15.5.12
Node: v25.2.1
macOS arm64

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