fix(windows): run bundled openclaw CLI/TUI via node.exe to restore terminal input (#571)
This commit is contained in:
@@ -172,27 +172,54 @@ function patchBrokenModules(nodeModulesDir) {
|
||||
}
|
||||
}
|
||||
|
||||
// https-proxy-agent@8.x only defines exports.import (ESM) with no CJS
|
||||
// fallback. The openclaw Gateway loads it via require(), which triggers
|
||||
// ERR_PACKAGE_PATH_NOT_EXPORTED. Patch exports to add CJS conditions.
|
||||
// https-proxy-agent: add a CJS `require` condition only when we can point to
|
||||
// a real CommonJS entry. Mapping `require` to an ESM file can cause
|
||||
// ERR_REQUIRE_CYCLE_MODULE in Node.js CLI/TUI flows.
|
||||
const hpaPkgPath = join(nodeModulesDir, 'https-proxy-agent', 'package.json');
|
||||
if (existsSync(hpaPkgPath)) {
|
||||
try {
|
||||
const { existsSync: fsExistsSync } = require('fs');
|
||||
const raw = readFileSync(hpaPkgPath, 'utf8');
|
||||
const pkg = JSON.parse(raw);
|
||||
const exp = pkg.exports;
|
||||
// Only patch if exports exists and lacks a CJS 'require' condition
|
||||
if (exp && exp.import && !exp.require && !exp['.']) {
|
||||
const hasRequireCondition = Boolean(
|
||||
(exp && typeof exp === 'object' && exp.require) ||
|
||||
(exp && typeof exp === 'object' && exp['.'] && exp['.'].require)
|
||||
);
|
||||
|
||||
const pkgDir = dirname(hpaPkgPath);
|
||||
const mainEntry = typeof pkg.main === 'string' ? pkg.main : null;
|
||||
const dotImport = exp && typeof exp === 'object' && exp['.'] && typeof exp['.'].import === 'string'
|
||||
? exp['.'].import
|
||||
: null;
|
||||
const rootImport = exp && typeof exp === 'object' && typeof exp.import === 'string'
|
||||
? exp.import
|
||||
: null;
|
||||
const importEntry = dotImport || rootImport;
|
||||
|
||||
const cjsCandidates = [
|
||||
mainEntry,
|
||||
importEntry && importEntry.endsWith('.js') ? importEntry.replace(/\.js$/, '.cjs') : null,
|
||||
'./dist/index.cjs',
|
||||
].filter(Boolean);
|
||||
|
||||
const requireTarget = cjsCandidates.find((candidate) =>
|
||||
fsExistsSync(join(pkgDir, candidate)),
|
||||
);
|
||||
|
||||
// Only patch if exports exists, lacks a CJS `require` condition, and we
|
||||
// have a verified CJS target file.
|
||||
if (exp && !hasRequireCondition && requireTarget) {
|
||||
pkg.exports = {
|
||||
'.': {
|
||||
import: exp.import,
|
||||
require: exp.import, // ESM dist works for CJS too via Node.js interop
|
||||
default: typeof exp.import === 'string' ? exp.import : exp.import.default,
|
||||
import: importEntry || requireTarget,
|
||||
require: requireTarget,
|
||||
default: importEntry || requireTarget,
|
||||
},
|
||||
};
|
||||
writeFileSync(hpaPkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
|
||||
count++;
|
||||
console.log('[after-pack] 🩹 Patched https-proxy-agent exports for CJS compatibility');
|
||||
console.log(`[after-pack] 🩹 Patched https-proxy-agent exports for CJS compatibility (require=${requireTarget})`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[after-pack] ⚠️ Failed to patch https-proxy-agent:', err.message);
|
||||
|
||||
Reference in New Issue
Block a user