問題描述
我正在開發一個電子應用程序,在該應用程序中,我使用 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 應用程序中執行命令時找不到命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!