Skip to content

fix: detect Node via process.type to keep Electron main process working#1033

Open
claygeo wants to merge 1 commit into
electric-sql:mainfrom
claygeo:fix/electron-in-node-main-process
Open

fix: detect Node via process.type to keep Electron main process working#1033
claygeo wants to merge 1 commit into
electric-sql:mainfrom
claygeo:fix/electron-in-node-main-process

Conversation

@claygeo

@claygeo claygeo commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #951 (thanks for merging that one!) and #813.

#951 fixed the Electron renderer crash by adding !process.versions.electron to IN_NODE. While verifying it I noticed that guard also flips IN_NODE to false in the Electron main and utility processes, because process.versions.electron is set in every Electron process, not just the renderer. Those are real Node environments, so PGlite then takes the browser path and loses the Node.js fs code path it needs. Running PGlite in the main process for filesystem-backed storage (keeping DB access out of the renderer) is a common pattern, so this is a regression.

This keys the check off process.type instead, which cleanly separates Electron's web contexts from its Node ones:

Environment process.type !process.versions.electron (#951) process.type exclusion (this PR)
Plain Node undefined Node ✅ Node ✅
Node worker_threads undefined Node ✅ Node ✅
Electron main browser browser ❌ Node ✅
Electron utility utility browser ❌ Node ✅
Electron renderer renderer browser ✅ browser ✅
Electron renderer web worker worker browser ✅ browser ✅
Electron renderer service worker service-worker browser ✅ browser ✅
Browser (no process) n/a browser ✅ browser ✅

So it still fixes the original renderer crash from #813, and additionally keeps the Node path working in the Electron main and utility processes.

process.type is Electron specific and not in @types/node, hence the small structural cast.

Checks run locally: typecheck, lint, and build pass for @electric-sql/pglite-utils. Changeset included (patch to @electric-sql/pglite).

The Electron guard added in electric-sql#951 (!process.versions.electron) also
disabled the Node code path in the Electron main and utility processes,
which are real Node environments that need it. process.versions.electron
is set in every Electron process, not just the renderer.

Key off process.type instead: exclude only Electron's web contexts
('renderer', 'worker', 'service-worker'), keeping the Node path for the
main ('browser') and utility processes, and for plain Node (no
process.type). Fixes the renderer crash from electric-sql#813 without regressing
PGlite in the Electron main process.

Refs electric-sql#951, electric-sql#813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant