0

After installing node-pty (an external module used to create pseudo terminals using node js) in a boilerplate electron-forge project; I found it throwing an error that some core module of node-pty is importing another module which nodejs is failing to find.

After some research I discovered that entry point of node-pty is src/index.js, which imports another module called src/unixTerminal.js (this file is imported if the system is running on linux platform and my PC is running on Ubuntu 20.04) and that module tries to import build/Releases/pty.node.js (unixTerminal.js calls many functions imported from pty.node.js, so this package cannot be ommitted) but as a matter of fact build/Releases/pty.node.js is missing and completely absent in the node_modules/node-pty folder of my project where I had installed node-pty

Why does this happen? Is this any fault of myself in installing node-pty, I had installed it directly using npm i command? If a vital file of a module is missing how can it work? Please tell me how can I use node-pty on Linux and why build/Releases/pty.node.js is missing in node-pty's directory?

  • How did you determine a file called `build/Releases/pty.node.js` would be imported? – AKX Sep 02 '22 at 11:10
  • I read the source code of node-pty from the node modules folder of my app, found its entry point and from there I discovered that the entry point file of node-pty requires another file called unixTerminal.js and after researching I understood that unixTerminal.js requires pty.node.js which is not present. – Hit and Run Sep 02 '22 at 11:13
  • [This line](https://github.com/microsoft/node-pty/blob/df1f411b5cad0503e1ea8abf177105bdb56f99e3/src/unixTerminal.ts#L16)? You read that wrong – it's looking for `pty.node`; `.node` is the extension for native Node extension modules. – AKX Sep 02 '22 at 11:16
  • Also: what was the output when you installed `node-pty` using `npm i`? Are you sure there weren't any errors? – AKX Sep 02 '22 at 11:16
  • Unix terminal is looking for `../build/Releases/pty.node`, are native node modules actually located deeply nested in such weird path. The require function of unixTerminal has "../" at its beginning which signifies that it is a custom node module not any core node module – Hit and Run Sep 02 '22 at 11:19
  • What I meant with "native node extension module" is "binary module that Node loads" as opposed to "JavaScript module". And yes, a library could include its extension in such a path if it likes. – AKX Sep 02 '22 at 11:20
  • I tried installing `node-pty` in a fresh VM environment, and it installs and works just fine, and `node_modules/node-pty/build/Release/pty.node` is present. – AKX Sep 02 '22 at 11:21
  • There were no errors while installing and I could also import the module into my app but the problem starts to occur when we use any function of that module— it simply throws errors that `build/Releases/pty.node` being required by `src/unixTerminal.js` was not found – Hit and Run Sep 02 '22 at 11:21
  • Well, have you tried uninstalling and reinstalling that module? – AKX Sep 02 '22 at 11:23
  • But in my case the file is not present,please help. I have been installing it 3 times and all the time it fails with the same error. Did you install that on Linux machine or anything else? If no,I guess node-pty has some different way of installation on linux. And also check once that is the spawn function of that library is working on your system – Hit and Run Sep 02 '22 at 11:23
  • Yes I have been installing and uninstalling it 3 times – Hit and Run Sep 02 '22 at 11:24
  • And sorry, I didn't have much knowledge about "native node extension module", so I asked questions like that – Hit and Run Sep 02 '22 at 11:25
  • In a `node:16` Docker container (so yes, Linux): `npm i node-pty && node -e 'require("node-pty").spawn()'`: `added 2 packages, and audited 3 packages in 3s`; no error. – AKX Sep 02 '22 at 11:25
  • Which version of Node.js are you running, anyway? (`node --version`) Also, does this also happen in a fresh project? (`cd $(mktemp -d) && npm i node-pty && node -e 'require("node-pty").spawn()'`) – AKX Sep 02 '22 at 11:29
  • Which version of node-pty did you install? If you check node-pty's GitHub source code from npm, you would find that it has a post-install script that actually REMOVES all the files and folders from build/Releases folder. [Post-install script of node-pty GitHub is here](https://github.com/microsoft/node-pty/blob/main/scripts/post-install.js),[check it's package.json also](https://github.com/microsoft/node-pty/blob/main/package.json) – Hit and Run Sep 02 '22 at 11:33
  • `node-pty@0.10.1`. – AKX Sep 02 '22 at 11:34
  • Also, if you read that script carefully, you'll notice that list of `BUILD_FILES` are files _not_ to be removed. – AKX Sep 02 '22 at 11:35
  • I am running node 18 and I install node-pty into a electron forge project – Hit and Run Sep 02 '22 at 11:37
  • You know, it would have been extremely useful if you had said you're using Electron Forge in your original post. – AKX Sep 02 '22 at 11:38
  • Well, I definitely can see it not removing it, because it isn't removed on my machine. See my answer – this is related to your Electron Forge setup. – AKX Sep 02 '22 at 11:45

1 Answers1

1

Since you're using Electron Forge (a crucial detail omitted from the original post), according to this issue I found by googling "node-pty electron forge" you'll need to configure the Electron packager to unpack the pty.node file:

    asar: {
      unpack: '**/node_modules/node-pty/build/Release/*'
    },
AKX
  • 152,115
  • 15
  • 115
  • 172