Skip to content

DefinePlugin: obj.MISSING?.method() compiles to undefined() instead of short-circuiting #21822

Closed

Description

@jacob-work

Have you used AI?

Yes

Which part of webpack is affected?

optimization (tree-shaking, splitChunks, concatenation)

Bug Description

Starting in 5.108.0, this:

process.env.SOME_UNDEFINED_FLAG?.includes("foo");

compiles to this, which crashes:

undefined("foo");

Previously, it compiled correctly to:

({"NODE_ENV":"production"}).SOME_UNDEFINED_FLAG?.includes("foo");

Link to Minimal Reproduction and steps to reproduce

https://github.com/jacob-work/webpack-define-plugin-optional-chaining-repro

Steps:

  1. Run 'npm install'
  2. Run 'npm run build'
  3. Check the output in the dist folder, you get undefined("foo") which crashes (can be tried in Chrome console).

Expected Behavior

Undefined env variables on process.env can be accessed without crashing.

Actual Behavior

All string method operations on undefined process.env variables crash

Environment

⬡ System:
────────────────────────────────────────────────────────────────────────
  OS:      macOS 26.6
  CPU:     (10) arm64 Apple M1 Pro
  Memory:  461.42 MB / 32.00 GB

⬡ Binaries:
────────────────────────────────────────────────────────────────────────
  Node:  24.19.0 - /Users/jacob/.local/state/fnm_multishells/12069_1787554964945/bin/node
  Yarn:  1.22.22 - /opt/homebrew/bin/yarn
  npm:   12.0.2 - /Users/jacob/.local/state/fnm_multishells/12069_1787554964945/bin/npm

⬡ Browsers:
────────────────────────────────────────────────────────────────────────
  Brave Browser:  151.1.93.138
  Chrome:         151.0.7922.174
  Firefox:        145.0.2
  Safari:         26.6

⬡ Packages:
────────────────────────────────────────────────────────────────────────
  webpack:      5.109.2 → 5.109.2
  webpack-cli:  7.2.2 → 7.2.2

Is this a regression?

Yes (please specify version below)

Last Working Version

5.107.2

Additional Context

No response

Activity

  1. added theissue type on Aug 25, 2026
  2. added
    area: optimizationTree-shaking, splitChunks, concatenation, ids (lib/optimize, lib/ids)
    regressionWorked in a previous release; pair with the Affected version field
    on Aug 25, 2026
  3. added a commit that references this issue on Aug 25, 2026
    692f1a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: optimizationTree-shaking, splitChunks, concatenation, ids (lib/optimize, lib/ids)regressionWorked in a previous release; pair with the Affected version field

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions