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

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

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)