npm:在 Electron 应用程序中执行命令时找不到命令

npm: command not found when executing commands in Electron app(npm:在 Electron 应用程序中执行命令时找不到命令)
本文介绍了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 应用程序中执行命令时找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)