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 <noreply@anthropic.com>
Co-authored-by: Andy <119136210+AndyMik90@users.noreply.github.com>
This commit is contained in:
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user