npm: command not found when executing commands in Electron app(npm:在 Electron 应用程序中执行命令时找不到命令)
问题描述
我正在开发一个电子应用程序,在该应用程序中,我使用 child_process.exec
执行 shell 命令.我运行的命令之一是 npm run start
;这在开发环境中完美运行,但是当我为生产构建应用程序时,所有 npm
命令都失败并显示以下错误:
I'm working on an electron app and within the app, I execute shell commands using child_process.exec
. One of the commands I run is npm run start
; this works perfectly in a dev environment but when I build the application for production all npm
commands fail with showing the following error:
Error: Command failed: npm run start
/bin/sh: npm: command not found
at ChildProcess.exithandler (child_process.js:287)
at emitTwo (events.js:126)
at ChildProcess.emit (events.js:214)
at maybeClose (internal/child_process.js:925)
at Socket.stream.socket.on (internal/child_process.js:346)
at emitOne (events.js:116)
at Socket.emit (events.js:211)
at Pipe._handle.close [as _onclose] (net.js:554)
我尝试通过运行以下命令以调试模式运行应用程序 open MyApp.app/Contents/MacOS/MyApp
并且 npm
命令成功运行且没有错误.
I tried running the application in debug mode by running the following command open MyApp.app/Contents/MacOS/MyApp
and the npm
commands run successfully with no errors.
可能是什么问题?
推荐答案
打包后的应用程序中$PATH环境变量错误的问题,在开发中是可以的,因为应用程序是从终端启动的,它可以访问$BASH 配置文件.
The issue that the environment variable of $PATH is wrong inside the packaged app, it works in development because the application is launched from the terminal which gives it access to the $BASH profile.
为了解决这个问题,我使用了这个包 fix-path.我安装了软件包并在文件顶部添加了以下代码段
To solve this problem I used this package fix-path. I installed the package and added the following snippet at the top of the file
if (process.env.NODE_ENV === 'production') {
const fixPath = require('fix-path');
fixPath();
}
我在 GitHub 上浏览了这个 issue 后得出了这个答案.感谢@Seblor
I came to this answer after going through this issue on GitHub. Thanks to @Seblor
这篇关于npm:在 Electron 应用程序中执行命令时找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:npm:在 Electron 应用程序中执行命令时找不到命令
基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01