简体   繁体   English

如何在 Electron 中使用 node-pty 正确启动 shell 环境?

[英]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.我有一个 Electron 应用程序,它使用 node-pty 创建一个伪终端实例。 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.当我使用( electron. )从 vscode 终端启动应用程序时,一切都按预期工作。 Ie the shell can find all programs just like it would in cmd.exe or Terminal.app.即 shell 可以像在 cmd.exe 或 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.但是,如果我构建应用程序并通过双击打开它,我注意到我的路径不包含某些目录,例如/usr/local/bin/ ,这可能导致某些程序(例如brew )失败并显示“未找到”错误。

Notably, if I launch the electron app from a terminal on mac using open -a MyElectronApp everything works as expected.值得注意的是,如果我使用open -a MyElectronApp MyElectronApp 从 Mac 上的终端启动 electron 应用程序,一切都会按预期工作。

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".但是,由于这是一个跨平台的应用程序,我想避免做任何类似“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!我认为我的问题可能是由于对 shell 环境的误解/无知!

Faced same issue.面临同样的问题。 Used https://github.com/sindresorhus/fix-path二手https://github.com/sindresorhus/fix-path

process.env.PATH was not available for the electron process in production mode. process.env.PATH 不适用于生产模式下的 electron 进程。 The above fix-path fixes the issue上面fix-path解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在电子锻造项目中安装“node-pty”后,缺少名为 pty.node.js 的文件。 如何在 Linux 中安装 node-pty - A file called pty.node.js is missing after installing "node-pty" in a electron-forge project. How can I install node-pty in Linux 如何不使用 node-pty 和 xterm 在终端中打印命令? - How do I not print a command in the terminal using node-pty and xterm? 如何通过node-pty设置$ PATH作为在节点中扩展进程的一部分? - How can I set $PATH as part of spawing a process in node via node-pty? Node JS - 退格在 Node-Pty 终端中不起作用 - Node JS - Backspace not working in Node-Pty Terminal 电子和打字稿:如何在运行时正确处理模块? - Electron and Typescript: How do I handle modules at runtime correctly? 如何正确地将 PapaParse 添加到我的 Electron 项目中? - How do I correctly add PapaParse to my Electron project? 如何在 webview electron 中启用 Node.js 集成 - How do I enable Node.js integration in webview electron 如何在电子壳中做oauth 2.0 - How to do oauth 2.0 in electron shell 如何在 windows 中正确杀死由 node.js/electron 生成的 child_process? - How can I kill a child_process spwaned by node.js/electron correctly in windows? 如何使用 Electron 的 Shell API 打开保存在用户目录中的文件 - How do I open a file saved in the user directory using Electron's Shell API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM