简体   繁体   English

Electron “未捕获的引用错误:未定义要求”

[英]Electron “Uncaught ReferenceError: require is not defined”

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>

But when I run this script I get error "Uncaught ReferenceError: require is not defined".但是,当我运行此脚本时,我收到错误“未捕获的 ReferenceError:未定义要求”。 I've tried to fix this for 3 days with no sucess.我试图解决这个问题 3 天没有成功。 I've enabled node intergration, installed browserify and read 10 diferrent explainations on how to do it with no success.我已经启用了节点集成,安装了 browserify 并阅读了 10 个不同的解释,说明如何做到这一点,但没有成功。 Do anybody know a fix for this or a alternative on executing a file?有人知道对此的修复或执行文件的替代方法吗?

See the Electron documentation .请参阅Electron 文档

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.您只能在进程中使用require (并使用child_process模块),但您正试图从渲染器进程中使用它。

Move it to the main process.将其移至主进程。

If you need to trigger the function from the renderer process (eg 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).如果您需要从渲染器进程触发 function(例如,当用户单击按钮时)然后使用ipcRenderer模块向主进程发送消息(并在那里有一个侦听器,它将调用verify function 以响应该问题信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM