Assignment to undeclared variable _process_env_key #2724

Closed
opened 2026-03-13 10:15:25 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @MarcJose on GitHub (Jan 17, 2026).

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. I created a Next.Js 16.1.3 app with the default settings and BetterAuth for the login
  2. Upon starting the app (without logging in) I receive the following message: assignment to undeclared variable _process_env_key
  3. Downgrading Next.Js to 16.1.1 makes it work again.

Current vs. Expected behavior

Current behaviour:

## Error Type
Runtime ReferenceError

## Error Message
assignment to undeclared variable _process_env_key


    at <unknown> (src/lib/auth/auth-client.ts:3:1)
    at <unknown> (src/components/Auth/ProtectedRoute.tsx:9:1)
    at RootLayout (src/app/layout.tsx:110:17)

## Code Frame
  1 | 'use client'
  2 |
> 3 | import { createAuthClient } from 'better-auth/react'
    | ^
  4 |
  5 | export const authClient = createAuthClient({
  6 |   baseURL: `${window.location.protocol}//${window.location.host}`,

Next.js version: 16.1.3 (Turbopack)

Expected behaviour:
It should be possible to set up the BetterAuth Client without errors.

What version of Better Auth are you using?

1.4.14

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Sun, 11 Jan 2026 17:10:53 +0000",
    "release": "6.18.5-arch1-1",
    "cpuCount": 16,
    "cpuModel": "AMD Ryzen 7 9800X3D 8-Core Processor",
    "totalMemory": "60.41 GB",
    "freeMemory": "46.43 GB"
  },
  "node": {
    "version": "v24.13.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.2"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.1.3"
    },
    {
      "name": "react",
      "version": "19.2.3"
    }
  ],
  "databases": [
    {
      "name": "@prisma/client",
      "version": "7.2.0"
    }
  ],
  "betterAuth": {
    "version": "1.4.14",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

'use client'

import { createAuthClient } from 'better-auth/react'

export const authClient = createAuthClient({
  baseURL: `${window.location.protocol}//${window.location.host}`,
})

// Export hooks and methods for convenience
export const { useSession, signIn, signOut, signUp } = authClient

Additional context

This only happens with the last two versions of Next.js. Downgrading does fix it.

Originally created by @MarcJose on GitHub (Jan 17, 2026). Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. I created a Next.Js 16.1.3 app with the default settings and BetterAuth for the login 2. Upon starting the app (without logging in) I receive the following message: `assignment to undeclared variable _process_env_key` 3. Downgrading Next.Js to 16.1.1 makes it work again. ### Current vs. Expected behavior Current behaviour: ``` ## Error Type Runtime ReferenceError ## Error Message assignment to undeclared variable _process_env_key at <unknown> (src/lib/auth/auth-client.ts:3:1) at <unknown> (src/components/Auth/ProtectedRoute.tsx:9:1) at RootLayout (src/app/layout.tsx:110:17) ## Code Frame 1 | 'use client' 2 | > 3 | import { createAuthClient } from 'better-auth/react' | ^ 4 | 5 | export const authClient = createAuthClient({ 6 | baseURL: `${window.location.protocol}//${window.location.host}`, Next.js version: 16.1.3 (Turbopack) ``` Expected behaviour: It should be possible to set up the BetterAuth Client without errors. ### What version of Better Auth are you using? 1.4.14 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Sun, 11 Jan 2026 17:10:53 +0000", "release": "6.18.5-arch1-1", "cpuCount": 16, "cpuModel": "AMD Ryzen 7 9800X3D 8-Core Processor", "totalMemory": "60.41 GB", "freeMemory": "46.43 GB" }, "node": { "version": "v24.13.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.6.2" }, "frameworks": [ { "name": "next", "version": "16.1.3" }, { "name": "react", "version": "19.2.3" } ], "databases": [ { "name": "@prisma/client", "version": "7.2.0" } ], "betterAuth": { "version": "1.4.14", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript 'use client' import { createAuthClient } from 'better-auth/react' export const authClient = createAuthClient({ baseURL: `${window.location.protocol}//${window.location.host}`, }) // Export hooks and methods for convenience export const { useSession, signIn, signOut, signUp } = authClient ``` ### Additional context This only happens with the last two versions of Next.js. Downgrading does fix it.
GiteaMirror added the nextjs label 2026-03-13 10:15:25 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 17, 2026):

It looks like you're encountering a Runtime ReferenceError: assignment to undeclared variable _process_env_key when trying to set up Better Auth with Next.js 16.1.3. This issue seems to be linked to the recent versions of Next.js, as downgrading to 16.1.1 resolves the problem.

For troubleshooting, you could consider the following steps:

  1. Check your environment variables: Ensure that any required environment variables referenced in your project (like process.env.*) are defined and correctly set. Missing environment variables can sometimes lead to such undeclared variable errors.

  2. Review the setup: Verify that the better-auth package is properly configured in your Next.js application. Look for any changes in the latest versions that might affect how it interacts with Next.js.

  3. Explore Open Issues: Since there’s a known tendency for issues to arise with specific versions of Next.js, keep an eye on related GitHub issues. Reporting your findings can help the development team address it if it’s not already documented.

Finally, if you still face issues, I would encourage you to tag @bekacru or @ping__ in your message for more targeted assistance from the Better Auth team.

Have you had a chance to check your environment variable settings?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 17, 2026): It looks like you're encountering a `Runtime ReferenceError: assignment to undeclared variable _process_env_key` when trying to set up Better Auth with Next.js 16.1.3. This issue seems to be linked to the recent versions of Next.js, as downgrading to 16.1.1 resolves the problem. For troubleshooting, you could consider the following steps: 1. **Check your environment variables**: Ensure that any required environment variables referenced in your project (like `process.env.*`) are defined and correctly set. Missing environment variables can sometimes lead to such undeclared variable errors. 2. **Review the setup**: Verify that the `better-auth` package is properly configured in your Next.js application. Look for any changes in the latest versions that might affect how it interacts with Next.js. 3. **Explore Open Issues**: Since there’s a known tendency for issues to arise with specific versions of Next.js, keep an eye on related GitHub issues. Reporting your findings can help the development team address it if it’s not already documented. Finally, if you still face issues, I would encourage you to tag @bekacru or @ping__ in your message for more targeted assistance from the Better Auth team. Have you had a chance to check your environment variable settings? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Jan 17, 2026):

This error is caused by a compatibility issue between BetterAuth and the latest Next.js (16.1.2/16.1.3), specifically when using Turbopack. The root cause is how Next.js now transpiles optional chaining on environment variable access (like globalThis.process?.env) in BetterAuth's env helper. The bundler emits a temporary variable (such as _process_env_key) but fails to declare it, resulting in the runtime ReferenceError you see. This is not a direct bug in your code or BetterAuth's client code, but rather a side effect of how Turbopack handles certain JavaScript patterns in recent Next.js versions reference.

Workarounds:

  • Run Next.js without Turbopack (e.g., use next dev instead of next dev --turbopack).
  • Downgrade Next.js to 16.1.1 or earlier, which does not have this issue.

There is no official fix in BetterAuth yet, but maintainers are actively updating environment variable handling and related code. For now, avoid Turbopack or stick to a compatible Next.js version until an upstream fix is available reference.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 17, 2026): <!-- Answer --> This error is caused by a compatibility issue between BetterAuth and the latest Next.js (16.1.2/16.1.3), specifically when using Turbopack. The root cause is how Next.js now transpiles optional chaining on environment variable access (like `globalThis.process?.env`) in BetterAuth's env helper. The bundler emits a temporary variable (such as `_process_env_key`) but fails to declare it, resulting in the runtime ReferenceError you see. This is not a direct bug in your code or BetterAuth's client code, but rather a side effect of how Turbopack handles certain JavaScript patterns in recent Next.js versions [reference](https://github.com/better-auth/better-auth/issues/2055). Workarounds: - Run Next.js without Turbopack (e.g., use `next dev` instead of `next dev --turbopack`). - Downgrade Next.js to 16.1.1 or earlier, which does not have this issue. There is no official fix in BetterAuth yet, but maintainers are actively updating environment variable handling and related code. For now, avoid Turbopack or stick to a compatible Next.js version until an upstream fix is available [reference](https://github.com/better-auth/better-auth/issues/2055). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20error%20is%20caused%20by%20a%20compatibility%20issue%20between%20BetterAuth%20and%20the%20latest%20Next.js%20%2816.1.2/16.1.3%29%2C%20specifically%20when%20using%20Turbopack.%20The%20root%20cause%20is%20how%20Next.js%20now%20transpiles%20optional%20chaining%20on%20environment%20variable%20access%20%28like%20%60globalThis.process%3F.env%60%29%20in%20BetterAuth%27s%20env%20helper.%20The%20bundler%20emits%20a%20temporary%20variable%20%28such%20as%20%60_process_env_key%60%29%20but%20fails%20to%20declare%20it%2C%20resulting%20in%20the%20runtime%20ReferenceError%20you%20see.%20This%20is%20not%20a%20direct%20bug%20in%20your%20code%20or%20BetterAuth%27s%20client%20code%2C%20but%20rather%20a%20side%20effect%20of%20how%20Turbopack%20handles%20certain%20JavaScript%20patterns%20in%20recent%20Next.js%20versions%C2%A0%5Breference%5D%28https%3A//github.com/better-auth/better-auth/issues/2055%29.%0A%0AWorkarounds%3A%0A-%20Run%20Next.js%20without%20Turbopack%20%28e.g.%2C%20use%20%60next%20dev%60%20instead%20of%20%60next%20dev%20--turbopack%60%29.%0A-%20Downgrade%20Next.js%20to%2016.1.1%20or%20earlier%2C%20which%20does%20not%20have%20this%20issue.%0A%0AThere%20is%20no%20official%20fix%20in%20BetterAuth%20yet%2C%20but%20maintainers%20are%20actively%20updating%20environment%20variable%20handling%20and%20related%20code.%20For%20now%2C%20avoid%20Turbopack%20or%20stick%20to%20a%20compatible%20Next.js%20version%20until%20an%20upstream%20fix%20is%20available%C2%A0%5Breference%5D%28https%3A//github.com/better-auth/better-auth/issues/2055%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/7435)
Author
Owner

@bytaesu commented on GitHub (Jan 17, 2026):

I will look into this 🧐

@bytaesu commented on GitHub (Jan 17, 2026): I will look into this 🧐
Author
Owner

@bytaesu commented on GitHub (Jan 18, 2026):

Image

Hi @MarcJose,

hmm.. I can't reproduce this with a minimal setup on my end.

Is this issue still happening? If so, could you share a reproducible repo with me? 🧐

@bytaesu commented on GitHub (Jan 18, 2026): > <img width="527" height="164" alt="Image" src="https://github.com/user-attachments/assets/8a6d4271-06e8-432d-bb25-fa0823ba2d42" /> Hi @MarcJose, hmm.. I can't reproduce this with a minimal setup on my end. Is this issue still happening? If so, could you share a reproducible repo with me? 🧐
Author
Owner

@hchatel commented on GitHub (Jan 22, 2026):

This error is caused by a compatibility issue between BetterAuth and the latest Next.js (16.1.2/16.1.3), specifically when using Turbopack. The root cause is how Next.js now transpiles optional chaining on environment variable access (like globalThis.process?.env) in BetterAuth's env helper. The bundler emits a temporary variable (such as _process_env_key) but fails to declare it, resulting in the runtime ReferenceError you see. This is not a direct bug in your code or BetterAuth's client code, but rather a side effect of how Turbopack handles certain JavaScript patterns in recent Next.js versions reference.

Workarounds:

* Run Next.js without Turbopack (e.g., use `next dev` instead of `next dev --turbopack`).

* Downgrade Next.js to 16.1.1 or earlier, which does not have this issue.

There is no official fix in BetterAuth yet, but maintainers are actively updating environment variable handling and related code. For now, avoid Turbopack or stick to a compatible Next.js version until an upstream fix is available reference.

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

Downgrading Next.js to 16.1.1 did the trick for me !

Many of my pages started failing after upgrading from Next 15 to 16.1.4 with similar error ReferenceError: assignment to undeclared variable _ref.

Thanks !

@hchatel commented on GitHub (Jan 22, 2026): > This error is caused by a compatibility issue between BetterAuth and the latest Next.js (16.1.2/16.1.3), specifically when using Turbopack. The root cause is how Next.js now transpiles optional chaining on environment variable access (like `globalThis.process?.env`) in BetterAuth's env helper. The bundler emits a temporary variable (such as `_process_env_key`) but fails to declare it, resulting in the runtime ReferenceError you see. This is not a direct bug in your code or BetterAuth's client code, but rather a side effect of how Turbopack handles certain JavaScript patterns in recent Next.js versions [reference](https://github.com/better-auth/better-auth/issues/2055). > > Workarounds: > > * Run Next.js without Turbopack (e.g., use `next dev` instead of `next dev --turbopack`). > > * Downgrade Next.js to 16.1.1 or earlier, which does not have this issue. > > > There is no official fix in BetterAuth yet, but maintainers are actively updating environment variable handling and related code. For now, avoid Turbopack or stick to a compatible Next.js version until an upstream fix is available [reference](https://github.com/better-auth/better-auth/issues/2055). > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/f11f2c1f-715e-4d5b-98b7-b34cd8f708c1?feedback_type=other) > > [![Chat with Dosu](https://camo.githubusercontent.com/7c571478962a6b8a5d47cac74b6824c75e4f6731bba52377e6007f9150462923/68747470733a2f2f646f73752e6465762f646f73752d636861742d62616467652e737667)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github) [![Open in Cursor](https://camo.githubusercontent.com/bdf7dbded65ee585887f47e5fd6fa099ea0933088b4b03d02e83b4def50154d4/68747470733a2f2f646f73752e6465762f646f73752d696e2d637572736f722e737667)](https://cursor.com/link/prompt?text=This%20error%20is%20caused%20by%20a%20compatibility%20issue%20between%20BetterAuth%20and%20the%20latest%20Next.js%20%2816.1.2/16.1.3%29%2C%20specifically%20when%20using%20Turbopack.%20The%20root%20cause%20is%20how%20Next.js%20now%20transpiles%20optional%20chaining%20on%20environment%20variable%20access%20%28like%20%60globalThis.process%3F.env%60%29%20in%20BetterAuth%27s%20env%20helper.%20The%20bundler%20emits%20a%20temporary%20variable%20%28such%20as%20%60_process_env_key%60%29%20but%20fails%20to%20declare%20it%2C%20resulting%20in%20the%20runtime%20ReferenceError%20you%20see.%20This%20is%20not%20a%20direct%20bug%20in%20your%20code%20or%20BetterAuth%27s%20client%20code%2C%20but%20rather%20a%20side%20effect%20of%20how%20Turbopack%20handles%20certain%20JavaScript%20patterns%20in%20recent%20Next.js%20versions%C2%A0%5Breference%5D%28https%3A//github.com/better-auth/better-auth/issues/2055%29.%0A%0AWorkarounds%3A%0A-%20Run%20Next.js%20without%20Turbopack%20%28e.g.%2C%20use%20%60next%20dev%60%20instead%20of%20%60next%20dev%20--turbopack%60%29.%0A-%20Downgrade%20Next.js%20to%2016.1.1%20or%20earlier%2C%20which%20does%20not%20have%20this%20issue.%0A%0AThere%20is%20no%20official%20fix%20in%20BetterAuth%20yet%2C%20but%20maintainers%20are%20actively%20updating%20environment%20variable%20handling%20and%20related%20code.%20For%20now%2C%20avoid%20Turbopack%20or%20stick%20to%20a%20compatible%20Next.js%20version%20until%20an%20upstream%20fix%20is%20available%C2%A0%5Breference%5D%28https%3A//github.com/better-auth/better-auth/issues/2055%29.) [![Join Discord](https://camo.githubusercontent.com/7d2066700925db1e370d956834f9ea0e5bc92cecbe84963fbd2a363e45091b4e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/a8bd7f3fcf3f5e3bd124eee9ae50ae2ac17b40971726665ac2121d48f9aee155/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/7435) Downgrading Next.js to 16.1.1 did the trick for me ! Many of my pages started failing after upgrading from Next 15 to 16.1.4 with similar error `ReferenceError: assignment to undeclared variable _ref`. Thanks !
Author
Owner

@MarcJose commented on GitHub (Jan 28, 2026):

Sorry for getting back now, it was quite busy here. The error seems to have gone with the most recent Next.Js version again (v16.1.6)

@MarcJose commented on GitHub (Jan 28, 2026): Sorry for getting back now, it was quite busy here. The error seems to have gone with the most recent Next.Js version again (v16.1.6)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2724