問題描述
我正在開發(fā)一個(gè)電子應(yīng)用程序,在該應(yīng)用程序中,我使用 child_process.exec
執(zhí)行 shell 命令.我運(yùn)行的命令之一是 npm run start
;這在開發(fā)環(huán)境中完美運(yùn)行,但是當(dāng)我為生產(chǎn)構(gòu)建應(yīng)用程序時(shí),所有 npm
命令都失敗并顯示以下錯(cuò)誤:
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)
我嘗試通過運(yùn)行以下命令以調(diào)試模式運(yùn)行應(yīng)用程序 open MyApp.app/Contents/MacOS/MyApp
并且 npm
命令成功運(yùn)行且沒有錯(cuò)誤.
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.
可能是什么問題?
推薦答案
打包后的應(yīng)用程序中$PATH環(huán)境變量錯(cuò)誤的問題,在開發(fā)中是可以的,因?yàn)閼?yīng)用程序是從終端啟動(dòng)的,它可以訪問$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.
為了解決這個(gè)問題,我使用了這個(gè)包 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 上瀏覽了這個(gè) issue 后得出了這個(gè)答案.感謝@Seblor
I came to this answer after going through this issue on GitHub. Thanks to @Seblor
這篇關(guān)于npm:在 Electron 應(yīng)用程序中執(zhí)行命令時(shí)找不到命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!