电子“未捕获的参考错误:未定义". [英] Electron "Uncaught ReferenceError: require is not defined"

查看:55
本文介绍了电子“未捕获的参考错误:未定义".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下脚本执行文件:

I'm trying to execute a file with this script:

 <script>
      function verify() {
    var child = require('child_process').execFile;
  var executablePath = "C:\\file";
  
  child(executablePath, function(err, data) {
  if(err){
     console.error(err);
     return;
  }
  
  console.log(data.toString());
    });
  }
    </script>

但是当我运行该脚本时,出现错误未捕获的ReferenceError:未定义require".我已尝试解决此问题3天,但均未成功.我已经启用了节点集成,安装了browserify并阅读了10个不同的说明,说明如何做到这一点都没有成功.有人知道解决此问题的方法或执行文件的替代方法吗?

But when I run this script I get error "Uncaught ReferenceError: require is not defined". I've tried to fix this for 3 days with no sucess. I've enabled node intergration, installed browserify and read 10 diferrent explainations on how to do it with no success. Do anybody know a fix for this or a alternative on executing a file?

推荐答案

请参见电子文档.

您只能在 main 进程中使用 require (并使用 child_process 模块),但是您正在尝试通过渲染器过程.

You can only use require (and use the child_process module) from the main process, but you are trying to use it from the renderer process.

将其移至主要流程.

如果您需要从渲染器过程中触发功能(例如,当用户单击按钮时),请使用 ipcRenderer 模块,用于向主进程发送消息(并在其中有一个侦听器,该侦听器将调用 verify 函数以响应该消息).

If you need to trigger the function from the renderer process (e.g. when the user clicks on a button) then use the ipcRenderer module to send a message to the main process (and have a listener there which will call the verify function in response to that message).

这篇关于电子“未捕获的参考错误:未定义".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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