require node-pty results in TypeError: Object.setPrototypeOf: expected an object or null, got undefined

267 Views Asked by At

TL;DR: If I try to do var pty = require('node-pty'); results in TypeError: Object.setPrototypeOf: expected an object or null, got undefined keep reading for context

Hi, I'm trying to build a proof of concept by creating a terminal using React. For that, I used xterm-for-react which I made it work fine, and node-pty with this last library is with the one I'm having problems. Initially I created a file in which I would try to make calls to it, it looks like this:

var os = require('os');
var pty = require('node-pty');

var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

var ptyProcess;

function createNewTerminal(FE){
    ptyProcess = pty.spawn(shell, [], {
        name: 'xterm-color',
        cols: 80,
        rows: 30,
        cwd: process.env.HOME,
        env: process.env
      });
    ptyProcess.onData((data) => FE.write(data));
}

function writeOnTerminal(data){
    ptyProcess.write(data);
}

module.exports = {
    createNewTerminal,
    writeOnTerminal
}

I know it may not be the best code out there, but I was doing it just to try to see if this was possible. My plan was to call the functions from the react component like this:

    import {createNewTerminal, writeOnTerminal} from './terminal-backend';
    function BashTerminal() {

    const xtermRef = React.useRef(null)

    React.useEffect(() => {
        // You can call any method in XTerm.js by using 'xterm xtermRef.current.terminal.[What you want to call]
        xtermRef.current.terminal.writeln("Hello, World!")
        createNewTerminal(xtermRef.current.terminal)
    }, [])

    const onData = (data) => {
        writeOnTerminal(data);
    }

    return (
        <XTerm ref={xtermRef} onData={onData}/>
    );
  }

But I was surprised that this was not working, and returned the error in the title. So, in order to reduce noise, I tried to change my functions to just console logs and just stay with the requires. My file now looked like this:

var os = require('os');
var pty = require('node-pty');

function createNewTerminal(FE){
    console.log("Creating new console");
}

function writeOnTerminal(data){
    console.log("Writing in terminal");
}

module.exports = {
    createNewTerminal,
    writeOnTerminal
}

Still got the same error. I'm currently not sure if this is even possible to do, or why this error occurs. Trying to look things online doesn't give any results, or maybe it does and I'm just not doing it right. Well, thanks for reading, I'm completely lost, so, if someone knows something even if it's not the complete answer I will be very thankful

0

There are 0 best solutions below