Skip to content

fix: process undefined in ESM preload when contextIsolation is disabled - #51693

Merged
ckerr merged 1 commit into
electron:mainfrom
sheitabrk:fix/esm-preload-process-globals
May 20, 2026
Merged

ckerr merged 1 commit into
electron:mainfrom
sheitabrk:fix/esm-preload-process-globals

Conversation

@sheitabrk

@sheitabrk sheitabrk commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

Fixes #46142.

When nodeIntegration and contextIsolation are both disabled, the Node
globals (process, Buffer, setImmediate, ...) were being removed from the
renderer's main world via a process.once('loaded', ...) listener. The
'loaded' event fires on the next tick after lib/renderer/init.ts returns,
which is before any async ESM preload script has finished executing — so by
the time the preload code ran, process was already gone and any access to
it threw ReferenceError. CJS preloads were unaffected because they ran
synchronously inside init.ts, before 'loaded' fired.

This change drops the 'loaded'-based deletion and instead runs it
explicitly in an onPreloadsLoaded callback that is invoked right before
appCodeLoaded() in both the ESM-preload path (via .finally(), so it
still runs even if a preload throws) and the no-ESM-preload path. The
deletion still happens after every preload (CJS or ESM) has had a chance
to use the Node globals, and still before the renderer's user-facing
scripts run — preserving the existing security boundary.

Checklist

Release Notes

Notes: Fixed an issue where process and other Node globals were undefined in ESM preload scripts when contextIsolation was disabled.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label May 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains unsigned commits. This repository enforces commit signatures
for all incoming PRs. To get your PR merged, please sign those commits
(git rebase --exec 'git commit -S --amend --no-edit -n' @{upstream}) and force push them to this branch
(git push --force-with-lease)

For more information on signing commits, see GitHub's documentation on Telling Git about your signing key.

@github-actions github-actions Bot added the needs-signed-commits Currently some or all of the commits in this PR are not signed label May 18, 2026
@sheitabrk
sheitabrk force-pushed the fix/esm-preload-process-globals branch from 8f70e5a to 64b6fc9 Compare May 18, 2026 23:34
@github-actions github-actions Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label May 18, 2026
@dsanders11 dsanders11 added semver/patch backwards-compatible bug fixes target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. labels May 19, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label May 19, 2026
@ckerr
ckerr merged commit 29cd2d5 into electron:main May 20, 2026
132 of 135 checks passed
@welcome

welcome Bot commented May 20, 2026

Copy link
Copy Markdown

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk

release-clerk Bot commented May 20, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed an issue where process and other Node globals were undefined in ESM preload scripts when contextIsolation was disabled.

@trop

trop Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels May 20, 2026
@trop

trop Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #51723

@trop trop Bot added in-flight/43-x-y and removed target/43-x-y PR should also be added to the "43-x-y" branch. labels May 20, 2026
@trop

trop Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

@ckerr has manually backported this PR to "main", please check out #51725

@trop

trop Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

@ckerr has manually backported this PR to "42-x-y", please check out #51726

@trop trop Bot added in-flight/42-x-y merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels May 20, 2026
ckerr added a commit that referenced this pull request May 21, 2026
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

44-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

No open projects
Status: No status

Development

Successfully merging this pull request may close these issues.

"ReferenceError: process is not defined" when using ESM mode and contextIsolation is not enabled

3 participants