fix: DevTools auto npm install for frontend on Windows

Two-part fix for frontend startup failure:
1. Check .package-lock.json as install-complete marker instead of
   just node_modules directory existence, and delete corrupt
   node_modules before reinstalling
2. Bypass npm.cmd batch file on Windows — use node with full path
   to npm-cli.js to avoid module resolution conflicts when cwd
   contains a node_modules directory

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-22 21:18:08 +08:00
parent e4d2eab9ad
commit ccd59db5b8
+27
View File
@@ -169,6 +169,33 @@ class ProcessManager extends EventEmitter {
args = svc.args || [];
}
// 对使用 npm 的服务,检查 node_modules 是否完整
if (svc.command === 'npx' || svc.command === 'node') {
const modulesDir = path.join(svc.cwd, 'node_modules');
const installMarker = path.join(modulesDir, '.package-lock.json');
if (!fs.existsSync(installMarker)) {
// 如果 node_modules 目录存在但不完整,先删除
if (fs.existsSync(modulesDir)) {
this.emit('log', serviceId, 'system', 'node_modules 不完整,正在清理...');
fs.rmSync(modulesDir, { recursive: true, force: true });
}
this.emit('log', serviceId, 'system', '正在运行 npm install...');
// Windows: npm.cmd batch file has module resolution issues when
// cwd has a node_modules directory, even if empty. Use node+npm-cli.js directly.
const installCmd = isWin
? `"${process.execPath}" "${path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js')}" install`
: 'npm install';
try {
execSync(installCmd, { cwd: svc.cwd, timeout: 120000, stdio: 'pipe' });
this.emit('log', serviceId, 'system', 'npm install 完成');
} catch (err) {
const stderr = err.stderr?.toString() || err.message;
this.emit('log', serviceId, 'error', `npm install 失败: ${stderr}`);
throw new Error(`npm install 失败: ${stderr}`);
}
}
}
const env = { ...process.env, ...svc.env };
// .cmd/.bat on Windows needs shell:true
const needsShell = isWin && (command.endsWith('.cmd') || command.endsWith('.bat'));