Search code examples
javascriptnode.jsterminalelectronnode-pty

How do I correctly launch a shell environment with node-pty in Electron?


I have an Electron App that's using node-pty to create a pseudo-terminal instance. This instance is created as follows:

const pty = require('node-pty');
const os = require('os');
const shell = process.env[os.platform() === 'win32' ? 'COMSPEC' : 'SHELL'];
const ptyProcess = pty.spawn(shell, [], {
  name: 'xterm-color',
  cols: 100,
  rows: 40,
  cwd: process.env.HOME,
  env: process.env
}); 

When I launch the app from the vscode terminal using (electron .) everything works as expected. I.e. the shell can find all programs just like it would in cmd.exe or Terminal.app.

However, if I build the app, and open it via double clicking on it, I notice that my path does not contain certain directories such as /usr/local/bin/, which can cause certain programs (like brew) to fail with "Not found" errors.

Notably, if I launch the electron app from a terminal on mac using open -a MyElectronApp everything works as expected.

I suspect my app needs to initialize the path differently somehow. However, since this is a cross-platform app, I'd like to avoid doing anything like "if os.platform == 'darwin' load /etc/paths".

Any help would be greatly appreciated. Please let me know if you need more information. I assume my issue is probably due to a misunderstanding/ignorance of shell environments!


Solution

  • Faced same issue. Used https://github.com/sindresorhus/fix-path

    process.env.PATH was not available for the electron process in production mode. The above fix-path fixes the issue