npm:在 Electron 应用程序中执行命令时找不到命令 [英] npm: command not found when executing commands in Electron app

查看:55
本文介绍了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 应用程序中执行命令时找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆