运行node-pty示例代码时报错
代码
var os = require('os');
var pty = require('node-pty');
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
var ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.onData((data) => {
process.stdout.write(data);
});
ptyProcess.write('ls\r');
ptyProcess.resize(100, 40);
ptyProcess.write('ls\r');
报错
Uncaught Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:456:14)
at WindowsTerminal._doWrite (C:\Users\Eugene\Documents\tmp\node_modules\node-pty\lib\windowsTerminal.js:115:30)
at WindowsTerminal._defer (C:\Users\Eugene\Documents\tmp\node_modules\node-pty\lib\windowsTerminal.js:157:24)
at WindowsTerminal._write (C:\Users\Eugene\Documents\tmp\node_modules\node-pty\lib\windowsTerminal.js:112:14)
at WindowsTerminal.Terminal.write (C:\Users\Eugene\Documents\tmp\node_modules\node-pty\lib\terminal.js:82:14) {
code: 'EPIPE'
主要出现错误的地方在ptyProcess.write
百度了2天都没找出问题所在,最后还是在github上找到了答案
Terminal input not working with ConPTY disabled(终端输入在禁用 ConPTY 的情况下不起作用)
本机内部版本号
显然不能使用ConPTY进行终端进程通信了
只能禁用掉ConPTY
Non-ConPTY PTYs fail on node 14(禁用掉ConPTY在node14下运行失败)
显然这个问题是由Node默认在 RW 模式下打开管道套接字引起的。
解决:Explicitly open winpty conin pipe in write-only mode(在只写模式下显式打开 winpty conin 管道)
I’ve just bisected Node versions on this and it starts with 14.0.0. 13.14.0 is the latest working one.
解决:将node 14 换成 node 13.14.0
问题解决!!