Skip to content

Empty NEXT_PUBLIC_* environment variables are evaluated as undefined rather than as empty strings #64832

Closed
@bryan-hoang

Description

@bryan-hoang

Link to the code that reproduces this issue

https://github.com/bryan-hoang/repro-undefined-next-public-env-vars

To Reproduce

  1. Run pnpm exec next dev
  2. Observe that the website is displaying process.env.NEXT_PUBLIC_FOO: undefined.

Current vs. Expected behavior

I expected process.env.NEXT_PUBLIC_FOO: to be displayed instead, since the .env file contains NEXT_PUBLIC_FOO=. The environment variable is defined as an empty string, but it's not being evaluated as one.

I find it surprising that environment variables that are defined as empty strings be treated as undefined.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #28~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 15 10:51:06 UTC 2
  Available memory (MB): 773973
  Available CPU cores: 48
Binaries:
  Node: 21.7.3
  npm: 10.5.2
  Yarn: N/A
  pnpm: 9.0.4
Relevant Packages:
  next: 14.3.0-canary.13 // Latest available version is detected (14.3.0-canary.13).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Output (export/standalone), Pages Router

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

next dev (local), next build (local), next start (local)

Additional context

It looks like this issue was first introduced in next@14.2.0-canary-14, as reverting to next@14.2.0-canary-13 fixes the issue.

After looking at the commits between those canary versions, I think the commit that introduced the issue is 76c9496. Specifically, this part of the diff: 76c9496#diff-28c0329619b90836564a7769c43ff473dbd28e8e31b75e650624da4d06911763R68.

Since empty strings are falsey, the environment variable doesn't end up being set.

The same guard against falsey values is present on the current canary version as of writing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    OutputRelated to the the output configuration option.Pages RouterRelated to Pages Router.bugIssue was opened via the bug report template.locked

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions