node.jselectronelectron-builderelectron-react-boilerplatenode-pty

Node-pty and ElectronJS


I am trying to use node-pty from within an ElectronJS app, but I'm struggling to get it to work.

I am using Electron React Boilerplate as the base for my project.

Repro of the issue:

git clone --depth 1 --branch main https://github.com/electron-react-boilerplate/electron-react-boilerplate.git test-electron-nodepty
cd test-electron-nodepty
npm i
cd release/app
npm i --save node-pty
cd ../..
npm i

(note: I installed node-pty in release/app per the instructions here for installing native dependencies in Electron React Boilerplate)

Then I update src/main/main.ts with the following:

import pty from 'node-pty';

pty.spawn('bash', [], {
  name: 'xterm-color',
  cols: 80,
  rows: 30,
  cwd: process.env.HOME,
  env: process.env,
});

Running npm run start, I get the error:

TypeError: Cannot read properties of undefined (reading 'spawn')
    at Object.<anonymous> (/Users/dcentore/Downloads/test-electron-nodepty/src/main/main.ts:35:5)
    at Module._compile (node:internal/modules/cjs/loader:1271:14)
    at Module.m._compile (/Users/dcentore/Downloads/test-electron-nodepty/node_modules/ts-node/src/index.ts:1618:23)
    at node:internal/modules/cjs/loader:1326:10
    at Object.require.extensions.<computed> [as .ts] (/Users/dcentore/Downloads/test-electron-nodepty/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1126:32)
    at node:internal/modules/cjs/loader:967:12
    at Function.<anonymous> (node:electron/js2c/asar_bundle:2:13377)
    at Function.Module._load (/Users/dcentore/Downloads/test-electron-nodepty/node_modules/runtime-required/runtime-required.js:34:23)
    at loadApplicationPackage (/Users/dcentore/Downloads/test-electron-nodepty/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:121:16)

I've also tried:

npm run build
node release/app/dist/main/main.js

And I get this error:

innerError Error: Cannot find module '../build/Debug/pty.node'
Require stack:
- /Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/unixTerminal.js
- /Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/index.js
- /Users/dcentore/Downloads/test-electron-nodepty/release/app/dist/main/main.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/unixTerminal.js:34:15)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/unixTerminal.js',
    '/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/index.js',
    '/Users/dcentore/Downloads/test-electron-nodepty/release/app/dist/main/main.js'
  ]
}
/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/unixTerminal.js:40
        throw outerError;
        ^

Error: The module '/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/build/Release/pty.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 116. This version of Node.js requires
NODE_MODULE_VERSION 115. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Module._extensions..node (node:internal/modules/cjs/loader:1327:18)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/Users/dcentore/Downloads/test-electron-nodepty/release/app/node_modules/node-pty/lib/unixTerminal.js:29:11)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12) {
  code: 'ERR_DLOPEN_FAILED'
}

I've spent a considerable amount of time trying solutions from changing package versions to using electron builder, but I can't seem to figure out the correct magic sauce to make everything work together.

I'm using an ARM Mac.


Solution

  • As per node-pyt's example usage, the import should be written as follows:

    import * as pty from 'node-pty';