Fork pseudoterminals in Node.JS
npm install node-pty2forkpty(3) bindings for node.js. This allows you to fork processes with pseudoterminal file descriptors. It returns a terminal object which allows reads and writes.
node-pty supports Linux, macOS and Windows. Windows support is possible by utilizing the winpty library.
node-pty powers many different terminal emulators, including:
js
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.on('data', function(data) {
console.log(data);
});
ptyProcess.write('ls\r');
ptyProcess.resize(100, 40);
ptyProcess.write('ls\r');
`
Building
`bash
Install dependencies and build C++
npm install
Compile TypeScript -> JavaScript
npm run tsc
`
Debugging
On Windows, you can show the winpty agent console window by adding the environment variable WINPTY_SHOW_CONSOLE=1 to the pty's environment. See https://github.com/rprichard/winpty#debugging-winpty for more information.
Troubleshooting
Powershell gives error 8009001d
> Internal Windows PowerShell error. Loading managed Windows PowerShell failed with error 8009001d.
This happens when PowerShell is launched with no SystemRoot` environment variable present.