From eb739afe9285ad699074b07eeaba1fd7f957b97e Mon Sep 17 00:00:00 2001 From: VDT-91 Date: Sun, 18 Jan 2026 10:15:37 +0100 Subject: [PATCH] fix(terminal): add require polyfill for ESM/Sentry compatibility (#1275) Terminal creation was failing with "ReferenceError: require is not defined" because: 1. Main process runs as ESM ("type": "module" in package.json) 2. Sentry uses require-in-the-middle which expects require.cache to exist 3. When node-pty tries to load native bindings via require(), Sentry's hook intercepts and tries to access require.cache which is undefined Fix: Add createRequire polyfill at the very top of index.ts, before any imports that might trigger Sentry's hooks. This provides a proper require function with require.cache that Sentry's instrumentation can use. Co-authored-by: Claude Opus 4.5 Co-authored-by: Andy <119136210+AndyMik90@users.noreply.github.com> --- apps/frontend/src/main/index.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/frontend/src/main/index.ts b/apps/frontend/src/main/index.ts index 6bfb08f4..eebbcc7c 100644 --- a/apps/frontend/src/main/index.ts +++ b/apps/frontend/src/main/index.ts @@ -1,3 +1,13 @@ +// Polyfill CommonJS require for ESM compatibility +// This MUST be at the very top, before any imports that might trigger Sentry's +// require-in-the-middle hooks. Sentry's hooks expect require.cache to exist, +// which is only available in CommonJS. Without this, node-pty native module +// loading fails with "ReferenceError: require is not defined". +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +// Make require globally available for Sentry's require-in-the-middle hooks +globalThis.require = require; + // Load .env file FIRST before any other imports that might use process.env import { config } from 'dotenv'; import { resolve, dirname } from 'path';